Skip to content

Commit

Permalink
Core: Warn when a module callback has a promise as a return value
Browse files Browse the repository at this point in the history
  • Loading branch information
raycohen committed May 16, 2021
1 parent cc8178f commit 5ac595b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,14 @@ function processModule( name, options, executeNow, modifiers = {} ) {
if ( objectType( executeNow ) === "function" ) {
moduleStack.push( module );
config.currentModule = module;
executeNow.call( module.testEnvironment, moduleFns );

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." );
}

moduleStack.pop();
module = module.parentModule || currentModule;
}
Expand Down
6 changes: 6 additions & 0 deletions test/cli/fixtures/async-module-warning/promise-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
QUnit.module( "module manually returning a promise", function() {
QUnit.test( "has a test", function( assert ) {
assert.true( true );
} );
return Promise.resolve( 1 );
} );
10 changes: 10 additions & 0 deletions test/cli/fixtures/async-module-warning/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// eslint-disable-next-line qunit/no-async-module-callbacks
QUnit.module( "module with async callback", async function() {
await Promise.resolve( 1 );

QUnit.test( "has a test", function( assert ) {
assert.true( true );
} );
} );

QUnit.module( "resulting parent module" );
18 changes: 18 additions & 0 deletions test/cli/fixtures/expected/tap-outputs.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,24 @@ ok 1 module providing hooks > module not providing hooks > has a test
# pass 1
# skip 0
# todo 0
# fail 0`,

"qunit async-module-warning/test.js":
`TAP version 13
ok 1 resulting parent module > has a test
1..1
# pass 1
# skip 0
# todo 0
# fail 0`,

"qunit async-module-warning/promise-test.js":
`TAP version 13
ok 1 module manually returning a promise > has a test
1..1
# pass 1
# skip 0
# todo 0
# fail 0`,

"qunit config-module.js":
Expand Down
18 changes: 18 additions & 0 deletions test/cli/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,24 @@ QUnit.module( "CLI Main", () => {
assert.equal( execution.stdout, expectedOutput[ command ] );
} );

QUnit.test( "warns about unsupported async module callback", async assert => {
const command = "qunit async-module-warning/test.js";
const execution = await execute( command );

assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "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.", "The warning shows" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );

QUnit.test( "warns about unsupported promise return value from module", async assert => {
const command = "qunit async-module-warning/promise-test.js";
const execution = await execute( command );

assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "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.", "The warning shows" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );

QUnit.module( "assert.expect failing conditions", () => {
QUnit.test( "mismatched expected assertions", async assert => {
const command = "qunit assert-expect/failing-expect.js";
Expand Down

0 comments on commit 5ac595b

Please sign in to comment.