Skip to content

Commit

Permalink
Core: Fix bad module nesting when module closure throws global error
Browse files Browse the repository at this point in the history
  • Loading branch information
Krinkle committed Aug 24, 2021
1 parent 1e6b1b2 commit e457e29
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
32 changes: 19 additions & 13 deletions src/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function processModule( name, options, executeNow, modifiers = {} ) {
options = undefined;
}

let module = createModule( name, options, modifiers );
const module = createModule( name, options, modifiers );

// Move any hooks to a 'hooks' object
const testEnvironment = module.testEnvironment;
Expand All @@ -79,24 +79,30 @@ function processModule( name, options, executeNow, modifiers = {} ) {
after: setHookFunction( module, "after" )
};

const currentModule = config.currentModule;
const prevModule = config.currentModule;
config.currentModule = module;

if ( objectType( executeNow ) === "function" ) {
moduleStack.push( module );
config.currentModule = module;

const cbReturnValue = executeNow.call( module.testEnvironment, moduleFns );
if ( cbReturnValue != null && objectType( cbReturnValue.then ) === "function" ) {
Logger.warn( "Returning a promise from a module callback is not supported. " +
"Instead, use hooks for async behavior. " +
"This will become an error in QUnit 3.0." );
try {
const cbReturnValue = executeNow.call( module.testEnvironment, moduleFns );
if ( cbReturnValue != null && objectType( cbReturnValue.then ) === "function" ) {
Logger.warn( "Returning a promise from a module callback is not supported. " +
"Instead, use hooks for async behavior. " +
"This will become an error in QUnit 3.0." );
}
} finally {

// If the module closure threw an uncaught error during the load phase,
// we let this bubble up to global error handlers. But, not until after
// we teardown internal state to ensure correct module nesting.
// Ref https://github.com/qunitjs/qunit/issues/1478.
moduleStack.pop();
config.currentModule = module.parentModule || prevModule;
}

moduleStack.pop();
module = module.parentModule || currentModule;
}

config.currentModule = module;

function setHookFromEnvironment( hooks, environment, name ) {
const potentialHook = environment[ name ];
hooks[ name ] = typeof potentialHook === "function" ? [ potentialHook ] : [];
Expand Down
2 changes: 1 addition & 1 deletion test/cli/fixtures/expected/tap-outputs.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ ok 3 module B > test D
"qunit module-nested.js":
`TAP version 13
ok 1 module 1 > test in module 1
ok 2 module 2 > module 3 > test in module 3
ok 2 module 3 > test in module 3
1..2
# pass 2
# skip 0
Expand Down

0 comments on commit e457e29

Please sign in to comment.