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.

Once before hooks have run, create a flattened deep copy of the module
testEnvironment and assign that to test testEnvironment. At this point nothing
should use test.testEnvironment until the before hooks have run.

Fixes #1328.
Ref #869.
Closes #1762.
  • Loading branch information
raycohen authored Jun 25, 2024
1 parent 57a543b commit 03b764d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 89 deletions.
4 changes: 2 additions & 2 deletions src/core/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ export function objectValuesSubset (obj, model) {
}

// Support: IE 11, iOS 7-8
export function extend (a, b, undefOnly) {
export function extend (a, b, undefOnly, allProperties) {
for (const prop in b) {
if (hasOwn.call(b, prop)) {
if (hasOwn.call(b, prop) || allProperties) {
if (b[prop] === undefined) {
delete a[prop];
} else if (!(undefOnly && typeof a[prop] !== 'undefined')) {
Expand Down
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
27 changes: 10 additions & 17 deletions src/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,6 @@ Test.prototype = {
return moduleStartChain.then(() => {
config.current = this;

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

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

queueHook (hook, hookName, hookOwner) {
const callHook = () => {
const promise = hook.call(this.testEnvironment, this.assert);
let promise;
if (hookName === 'before' || hookName === 'after') {
// before and after hooks are called with the owning module's testEnvironment
promise = hook.call(hookOwner.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 +483,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 @@ -507,7 +500,7 @@ Test.prototype = {
...test.hooks('before'),

function () {
test.preserveTestEnvironment();
test.testEnvironment = extend({}, test.module.testEnvironment, false, true);
},

...test.hooks('beforeEach'),
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
117 changes: 55 additions & 62 deletions test/main/modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,57 +16,51 @@ QUnit.module('QUnit.module', function () {
// parent > child > one
// parent > child > two
'parent-before: (empty)',
'child-before: beforeP=1',
'parent-beforeEach: beforeP=1 beforeC=1',
'child-beforeEach: beforeP=1 beforeC=1 beforeEach=P',
'child-test: beforeP=1 beforeC=1 beforeEach=PC',
'child-afterEach: beforeP=1 beforeC=1 beforeEach=PC tester=1',
'parent-afterEach: beforeP=1 beforeC=1 beforeEach=PC tester=1 afterEach=C',
'parent-beforeEach: beforeP=1 beforeC=1',
'child-beforeEach: beforeP=1 beforeC=1 beforeEach=P',
'child-test: beforeP=1 beforeC=1 beforeEach=PC',
'child-afterEach: beforeP=1 beforeC=1 beforeEach=PC tester=2',
'parent-afterEach: beforeP=1 beforeC=1 beforeEach=PC tester=2 afterEach=C',
'child-after: beforeP=1 beforeC=1 beforeEach=PC tester=2 afterEach=CP',
'parent-after: beforeP=1 beforeC=1 beforeEach=PC tester=2 afterEach=CP afterC=1'
'child-before: %beforeP=1',
'parent-beforeEach: beforeC=1 beforeP=1',
'child-beforeEach: beforeC=1 beforeP=1 beforeEach=P',
'child-test: beforeC=1 beforeP=1 beforeEach=PC',
'child-afterEach: beforeC=1 beforeP=1 beforeEach=PC tester=1',
'parent-afterEach: beforeC=1 beforeP=1 beforeEach=PC tester=1 afterEach=C',
'parent-beforeEach: beforeC=1 beforeP=1',
'child-beforeEach: beforeC=1 beforeP=1 beforeEach=P',
'child-test: beforeC=1 beforeP=1 beforeEach=PC',
'child-afterEach: beforeC=1 beforeP=1 beforeEach=PC tester=2',
'parent-afterEach: beforeC=1 beforeP=1 beforeEach=PC tester=2 afterEach=C',
'child-after: beforeC=1 %beforeP=1',
'parent-after: beforeP=1'
],
// FIXME: https://github.com/qunitjs/qunit/issues/1328
// - parent test missing own state if there is a child module before the test.
// - last test state presists into after()
'parent with trailing test': [
// parent > child > one
// parent > two
'parent-before: (empty)',
'child-before: beforeP=1',
'parent-beforeEach: beforeP=1 beforeC=1',
'child-beforeEach: beforeP=1 beforeC=1 beforeEach=P',
'child-test: beforeP=1 beforeC=1 beforeEach=PC',
'child-afterEach: beforeP=1 beforeC=1 beforeEach=PC tester=1',
'parent-afterEach: beforeP=1 beforeC=1 beforeEach=PC tester=1 afterEach=C',
'child-after: beforeP=1 beforeC=1 beforeEach=PC tester=1 afterEach=CP',
'parent-beforeEach: (empty)',
'parent-test: beforeEach=P',
'parent-afterEach: beforeEach=P tester=2',
'parent-after: beforeEach=P tester=2 afterEach=P'
'child-before: %beforeP=1',
'parent-beforeEach: beforeC=1 beforeP=1',
'child-beforeEach: beforeC=1 beforeP=1 beforeEach=P',
'child-test: beforeC=1 beforeP=1 beforeEach=PC',
'child-afterEach: beforeC=1 beforeP=1 beforeEach=PC tester=1',
'parent-afterEach: beforeC=1 beforeP=1 beforeEach=PC tester=1 afterEach=C',
'child-after: beforeC=1 %beforeP=1',
'parent-beforeEach: beforeP=1',
'parent-test: beforeP=1 beforeEach=P',
'parent-afterEach: beforeP=1 beforeEach=P tester=2',
'parent-after: beforeP=1'
],

// FIXME: https://github.com/qunitjs/qunit/issues/1328
// child is missing parent state if there is an initial test before the child module.
'parent with initial test': [
// parent > one
// parent > child > two
'parent-before: (empty)',
'parent-beforeEach: beforeP=1',
'parent-test: beforeP=1 beforeEach=P',
'parent-afterEach: beforeP=1 beforeEach=P tester=1',
'child-before: (empty)',
'parent-beforeEach: beforeC=1',
'child-beforeEach: beforeC=1 beforeEach=P',
'child-test: beforeC=1 beforeEach=PC',
'child-afterEach: beforeC=1 beforeEach=PC tester=2',
'parent-afterEach: beforeC=1 beforeEach=PC tester=2 afterEach=C',
'child-after: beforeC=1 beforeEach=PC tester=2 afterEach=CP',
'parent-after: beforeC=1 beforeEach=PC tester=2 afterEach=CP afterC=1'
'child-before: %beforeP=1',
'parent-beforeEach: beforeC=1 beforeP=1',
'child-beforeEach: beforeC=1 beforeP=1 beforeEach=P',
'child-test: beforeC=1 beforeP=1 beforeEach=PC',
'child-afterEach: beforeC=1 beforeP=1 beforeEach=PC tester=2',
'parent-afterEach: beforeC=1 beforeP=1 beforeEach=PC tester=2 afterEach=C',
'child-after: beforeC=1 %beforeP=1',
'parent-after: beforeP=1'
],

// Confirm each step waits for the previous before restoring/saving testEnvironment
Expand All @@ -78,34 +72,33 @@ QUnit.module('QUnit.module', function () {
'parent-beforeEach: beforeP=1',
'parent-test: beforeP=1 beforeEach=P',
'parent-afterEach: beforeP=1 beforeEach=P tester=1',
'child-before: (empty)',
'parent-beforeEach: beforeC=1',
'child-beforeEach: beforeC=1 beforeEach=P',
'child-test: beforeC=1 beforeEach=PC',
'child-afterEach: beforeC=1 beforeEach=PC tester=2',
'parent-afterEach: beforeC=1 beforeEach=PC tester=2 afterEach=C',
'child-after: beforeC=1 beforeEach=PC tester=2 afterEach=CP',
'parent-after: beforeC=1 beforeEach=PC tester=2 afterEach=CP afterC=1'
'child-before: %beforeP=1',
'parent-beforeEach: beforeC=1 beforeP=1',
'child-beforeEach: beforeC=1 beforeP=1 beforeEach=P',
'child-test: beforeC=1 beforeP=1 beforeEach=PC',
'child-afterEach: beforeC=1 beforeP=1 beforeEach=PC tester=2',
'parent-afterEach: beforeC=1 beforeP=1 beforeEach=PC tester=2 afterEach=C',
'child-after: beforeC=1 %beforeP=1',
'parent-after: beforeP=1'
],
'multiple hooks': [

'parent-before: (empty)',
'parent-before: beforeP=1',
'child-before: beforeP=12',
'child-before: beforeP=12 beforeC=1',
'parent-beforeEach: beforeP=12 beforeC=12',
'parent-beforeEach: beforeP=12 beforeC=12 beforeEach=P1',
'child-beforeEach: beforeP=12 beforeC=12 beforeEach=P1P2',
'child-beforeEach: beforeP=12 beforeC=12 beforeEach=P1P2C1',
'child-test: beforeP=12 beforeC=12 beforeEach=P1P2C1C2',
'child-afterEach: beforeP=12 beforeC=12 beforeEach=P1P2C1C2 tester=2',
'child-afterEach: beforeP=12 beforeC=12 beforeEach=P1P2C1C2 tester=2 afterEach=C2',
'parent-afterEach: beforeP=12 beforeC=12 beforeEach=P1P2C1C2 tester=2 afterEach=C2C1',
'parent-afterEach: beforeP=12 beforeC=12 beforeEach=P1P2C1C2 tester=2 afterEach=C2C1P2',
'child-after: beforeP=12 beforeC=12 beforeEach=P1P2C1C2 tester=2 afterEach=C2C1P2P1',
'child-after: beforeP=12 beforeC=12 beforeEach=P1P2C1C2 tester=2 afterEach=C2C1P2P1 afterC=2',
'parent-after: beforeP=12 beforeC=12 beforeEach=P1P2C1C2 tester=2 afterEach=C2C1P2P1 afterC=21',
'parent-after: beforeP=12 beforeC=12 beforeEach=P1P2C1C2 tester=2 afterEach=C2C1P2P1 afterC=21 afterP=2'
'child-before: %beforeP=12',
'child-before: beforeC=1 %beforeP=12',
'parent-beforeEach: beforeC=12 beforeP=12',
'parent-beforeEach: beforeC=12 beforeP=12 beforeEach=P1',
'child-beforeEach: beforeC=12 beforeP=12 beforeEach=P1P2',
'child-beforeEach: beforeC=12 beforeP=12 beforeEach=P1P2C1',
'child-test: beforeC=12 beforeP=12 beforeEach=P1P2C1C2',
'child-afterEach: beforeC=12 beforeP=12 beforeEach=P1P2C1C2 tester=2',
'child-afterEach: beforeC=12 beforeP=12 beforeEach=P1P2C1C2 tester=2 afterEach=C2',
'parent-afterEach: beforeC=12 beforeP=12 beforeEach=P1P2C1C2 tester=2 afterEach=C2C1',
'parent-afterEach: beforeC=12 beforeP=12 beforeEach=P1P2C1C2 tester=2 afterEach=C2C1P2',
'child-after: beforeC=12 %beforeP=12',
'child-after: beforeC=12 afterC=2 %beforeP=12',
'parent-after: beforeP=12',
'parent-after: beforeP=12 afterP=2'
]
};

Expand Down

0 comments on commit 03b764d

Please sign in to comment.