Skip to content

Commit

Permalink
Core: Run before and after hooks with module context
Browse files Browse the repository at this point in the history
The before and after hooks run once per module as long as there is at least one
test in the module. Using environment inheritance allows us to use the module
context in those hooks, which allows reading the expected changes to the
context from a before hook inside nested modules.

Fixes qunitjs#1328.
Ref qunitjs#869.
  • Loading branch information
raycohen committed Jun 21, 2024
1 parent da0c901 commit 6b2a5c3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ function createModule (name, testEnvironment, modifiers) {
const skip = (parentModule !== null && parentModule.skip) || modifiers.skip;
const todo = (parentModule !== null && parentModule.todo) || modifiers.todo;

const env = {};
let env = {};
if (parentModule) {
extend(env, parentModule.testEnvironment);
env = Object.create(parentModule.testEnvironment || {});
}
extend(env, testEnvironment);

Expand Down
29 changes: 9 additions & 20 deletions src/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ Test.prototype = {
return moduleStartChain.then(() => {
config.current = this;

this.testEnvironment = extend({}, module.testEnvironment);
this.testEnvironment = Object.create(module.testEnvironment || {});

this.started = performance.now();
emit('testStart', this.testReport.start(true));
Expand Down Expand Up @@ -251,17 +251,18 @@ Test.prototype = {

queueHook (hook, hookName, hookOwner) {
const callHook = () => {
const promise = hook.call(this.testEnvironment, this.assert);
let promise;
if (hookName === 'before' || hookName === 'after') {
promise = hook.call(this.module.testEnvironment, this.assert);
} else {
promise = hook.call(this.testEnvironment, this.assert);
}
this.resolvePromise(promise, hookName);
};

const runHook = () => {
if (hookName === 'before') {
if (hookOwner.testsRun !== 0) {
return;
}

this.preserveEnvironment = true;
if (hookName === 'before' && hookOwner.testsRun !== 0) {
return;
}

// The 'after' hook should only execute when there are not tests left and
Expand Down Expand Up @@ -483,13 +484,6 @@ Test.prototype = {
}
},

preserveTestEnvironment: function () {
if (this.preserveEnvironment) {
this.module.testEnvironment = this.testEnvironment;
this.testEnvironment = extend({}, this.module.testEnvironment);
}
},

queue () {
const test = this;

Expand All @@ -505,11 +499,6 @@ Test.prototype = {
},

...test.hooks('before'),

function () {
test.preserveTestEnvironment();
},

...test.hooks('beforeEach'),

function () {
Expand Down
16 changes: 10 additions & 6 deletions test/main/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,18 +242,22 @@ QUnit.module('assert.async', function () {
QUnit.test('test', function () {});
});

var inBeforeHookModuleState;
QUnit.module('in before hook', {
before: function (assert) {
var done = assert.async();
var testContext = this;
setTimeout(function () {
testContext.state = 'before';
inBeforeHookModuleState = 'before';
done();
});
}
}, function () {
QUnit.test('call order', function (assert) {
assert.equal(this.state, 'before', 'called before test callback');
assert.equal(
inBeforeHookModuleState,
'before',
'called before test callback'
);
});
});

Expand Down Expand Up @@ -289,18 +293,18 @@ QUnit.module('assert.async', function () {
});
});

var inAfterHookModuleState;
QUnit.module('in after hook', {
after: function (assert) {
assert.equal(this.state, 'done', 'called after test callback');
assert.equal(inAfterHookModuleState, 'done', 'called after test callback');
assert.true(true, 'called before expected assert count is validated');
}
}, function () {
QUnit.test('call order', function (assert) {
assert.expect(2);
var done = assert.async();
var testContext = this;
setTimeout(function () {
testContext.state = 'done';
inAfterHookModuleState = 'done';
done();
});
});
Expand Down

0 comments on commit 6b2a5c3

Please sign in to comment.