Skip to content

Commit

Permalink
Core: Throw an error when hooks are invoked outside of their module
Browse files Browse the repository at this point in the history
  • Loading branch information
raycohen committed Apr 5, 2021
1 parent 86fc89b commit 021956c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/module.js
Expand Up @@ -94,6 +94,9 @@ function processModule( name, options, executeNow, modifiers = {} ) {

function setHookFunction( module, hookName ) {
return function setHook( callback ) {
if ( config.currentModule !== module ) {
throw new Error( "Do not invoke hooks outside of their providing module." );
}
module.hooks[ hookName ].push( callback );
};
}
Expand Down
31 changes: 31 additions & 0 deletions test/main/modules.js
Expand Up @@ -419,4 +419,35 @@ QUnit.module( "QUnit.module", function() {
assert.expect( 9 );
} );
} );

QUnit.module( "Improperly invoked hooks", function( hooks ) {
QUnit.module( "Nested module with different hooks variable name", function( innerHooks ) {
this.outerHookRan = false;
try {
hooks.beforeEach( function() {
this.outerHookRan = true;
} );
} catch ( e ) {
this.beforeEachError = e;
}

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

QUnit.test( "calling parent module's beforeEach errors", function( assert ) {
assert.strictEqual( this.beforeEachError.message,
"Do not invoke hooks outside of their providing module." );
assert.false( this.outerHookRan );
assert.true( this.innerHookRan );
} );
} );

QUnit.test( "hooks error when invoked during test execution", function( assert ) {
assert.throws( function() {
hooks.beforeEach( function() { } );
}, "Do not invoke hooks outside of their providing module." );
} );
} );
} );

0 comments on commit 021956c

Please sign in to comment.