Error when a hook is invoked inside the wrong module
When a user forgets to provide the hooks param in a module callback, they may unintentionally install a hook on a different module than is visually indicated, eg:
module('outer', function (hooks) {
test('one', ...);
module('inner a', function () {
hooks.beforeEach(...); // will run before all three tests even though visually nested in "inner a"
test('two', ...);
});
module('inner b', function () {
test('three', ...);
});
});
There is a lint rule for this, but I think it would make sense for QUnit to throw an error when this occurs. Is there any reason a user would want to do this intentionally?
Yeah, I agree we could flag this automatically. Basically if hooks are called on during the top-level executeNow() phase of a module, we would throw an error if it is not for the current inner-most module. This would mean it would also throw if you for some reason intentionally named them differently and accessed it directly:
module('outer', hooks => {
test('one', …);
module('inner a', gizmos => {
gizmos.beforeEach(…);
hooks.beforeEach(…); // will throw Error
test('two', …);
});
});
I'd welcome this change in a PR, to land for QUnit 3. Preferably to first as a warning to land in QUnit 2.x, to aid in migration (or fixing of mistakes) prior to the upgrade.