Skip to content

Commit

Permalink
Core: Improve warning for incorrect hook usage to include module name
Browse files Browse the repository at this point in the history
Add the names of the relevant modules so the message is more actionable
for users who encounter it.

Fixes #1647.
  • Loading branch information
chriskrycho committed Aug 26, 2021
1 parent e457e29 commit 93eb8a8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,10 @@ function processModule( name, options, executeNow, modifiers = {} ) {
function setHookFunction( module, hookName ) {
return function setHook( callback ) {
if ( config.currentModule !== module ) {
Logger.warn( "The `" + hookName + "` hook was called inside the wrong module. " +
"Instead, use hooks provided by the callback to the containing 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." );
}
module.hooks[ hookName ].push( callback );
Expand Down
2 changes: 1 addition & 1 deletion test/cli/cli-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ CALLBACK: done`;
const execution = await execute( command );

assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "The `beforeEach` hook was called inside the wrong module. Instead, use hooks provided by the callback to the containing module. This will become an error in QUnit 3.0.", "The warning shows" );
assert.equal( execution.stderr, "The `beforeEach` hook was called inside the wrong module (`module providing hooks > module not providing hooks`). Instead, use hooks provided by the callback to the containing module (`module providing hooks`). This will become an error in QUnit 3.0.", "The warning shows" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );

Expand Down

0 comments on commit 93eb8a8

Please sign in to comment.