Skip to content

Commit

Permalink
CLI: Ensure an unhandled rejection results in a failed test.
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue committed Dec 18, 2017
1 parent 76188d3 commit 336b32e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
5 changes: 5 additions & 0 deletions bin/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ function run( args, options ) {
}
} );

// Listen for unhandled rejections, and call QUnit.onError.
process.on( "unhandledRejection", function( reason ) {
QUnit.onError( reason );
} );

const files = utils.getFilesFromArgs( args );

QUnit = requireQUnit();
Expand Down
1 change: 0 additions & 1 deletion reporter/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -979,5 +979,4 @@ export function escapeText( s ) {

return ret;
};

}() );
18 changes: 17 additions & 1 deletion test/cli/fixtures/expected/tap-outputs.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,22 @@ Available custom reporters from dependencies are: npm-reporter
/* eslint-disable max-len */
"qunit hanging-test": `Error: Process exited before tests finished running
Last test to run (hanging) has an async hold. Ensure all assert.async() callbacks are invoked and Promises resolve. You should also set a standard timeout via QUnit.config.testTimeout.
`
`,
/* eslint-enable max-len */
"qunit unhandled-rejection.js":
`TAP version 13
not ok 1 Unhandled Rejections > test passes just fine, but has a rejected promise
---
message: "Error thrown in non-returned promise!"
severity: failed
actual: null
expected: undefined
stack: undefined:undefined
...
1..1
# pass 0
# skip 0
# todo 0
# fail 1
`
};
19 changes: 19 additions & 0 deletions test/cli/fixtures/unhandled-rejection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use strict";

QUnit.module( "Unhandled Rejections", function() {
QUnit.test( "test passes just fine, but has a rejected promise", function( assert ) {
assert.ok( true );

const done = assert.async();

new Promise( function( resolve ) {
setTimeout( resolve );
} )
.then( function() {
throw new Error( "Error thrown in non-returned promise!" );
} );

// prevent test from exiting before unhandled rejection fires
setTimeout( done, 10 );
} );
} );
16 changes: 16 additions & 0 deletions test/cli/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ QUnit.module( "CLI Main", function() {
}
} ) );

QUnit.test( "unhandled rejections fail tests", co.wrap( function* ( assert ) {
const command = "qunit unhandled-rejection.js";

try {
const result = yield execute( command );
assert.pushResult( {
result: false,
actual: result.stdout
} );
} catch ( e ) {
assert.equal( e.code, 1 );
assert.equal( e.stderr, "" );
assert.equal( e.stdout, expectedOutput[ command ] );
}
} ) );

QUnit.module( "filter", function() {
QUnit.test( "can properly filter tests", co.wrap( function* ( assert ) {
const command = "qunit --filter 'single' test single.js 'glob/**/*-test.js'";
Expand Down

0 comments on commit 336b32e

Please sign in to comment.