Skip to content

Commit

Permalink
Core: Disallow creating hooks outside the current module
Browse files Browse the repository at this point in the history
Change deprecation warning into an error.

Ref #1576
Closes #1578

Co-authored-by: Ray Cohen <[email protected]>
  • Loading branch information
Krinkle and raycohen committed Jun 7, 2024
1 parent a7e10f6 commit bd0714d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 27 deletions.
6 changes: 1 addition & 5 deletions src/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,7 @@ function setHookFromEnvironment (hooks, environment, name) {
function makeSetHook (module, hookName) {
return function setHook (callback) {
if (config.currentModule !== module) {
Logger.warn('The `' + hookName + '` hook was called inside the wrong module (`' +
config.currentModule.name + '`). ' +
'Instead, use hooks provided by the callback to the containing module (`' +
module.name + '`). ' +
'This will become an error in QUnit 3.0.');
throw new Error(`Cannot add ${hookName} hooks outside the containing module. Called on "${module.name}", instead of expected "${config.currentModule.name}"`);
}
module.hooks[hookName].push(callback);
};
Expand Down
9 changes: 0 additions & 9 deletions test/cli/fixtures/hooks-other-module-warning.js

This file was deleted.

13 changes: 0 additions & 13 deletions test/cli/fixtures/hooks-other-module-warning.tap.txt

This file was deleted.

35 changes: 35 additions & 0 deletions test/main/modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,4 +419,39 @@ QUnit.module('QUnit.module', function () {
assert.expect(9);
});
});

QUnit.module('improper hook creation', function (hooks) {
QUnit.module('child', function (innerHooks) {
var outerHookRan = false;
var innerHookRan = false;
var beforeEachError = null;

try {
hooks.beforeEach(function () {
outerHookRan = true;
});
} catch (e) {
beforeEachError = e;
}

innerHooks.beforeEach(function () {
innerHookRan = true;
});

QUnit.test('create hook on parent module', function (assert) {
assert.deepEqual(beforeEachError, new Error(
'Cannot add beforeEach hooks outside the containing module. Called on "QUnit.module > improper hook creation", instead of expected "QUnit.module > improper hook creation > create hook for parent module"',
'beforeEachError'
));
assert.false(outerHookRan, 'outerHookRan');
assert.true(innerHookRan, 'innerHookRan');
});
});

QUnit.test('create hook during test', function (assert) {
assert.throws(function () {
hooks.beforeEach(function () { });
}, /Cannot add beforeEach hooks outside the containing module/);
});
});
});

0 comments on commit bd0714d

Please sign in to comment.