Skip to content

Commit

Permalink
Tests: Increase code coverage
Browse files Browse the repository at this point in the history
Increase code coverage for QUnit.equiv, QUnit.diff, test.js.

Closes #1590.
  • Loading branch information
smcclure15 committed Apr 26, 2021
1 parent db79953 commit dfdfd43
Show file tree
Hide file tree
Showing 20 changed files with 361 additions and 22 deletions.
36 changes: 16 additions & 20 deletions src/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,26 +528,24 @@ Test.prototype = {
const then = promise.then;
if ( objectType( then ) === "function" ) {
const resume = internalStop( test );
const resolve = function() { resume(); };
if ( config.notrycatch ) {
then.call( promise, function() { resume(); } );
then.call( promise, resolve );
} else {
then.call(
promise,
function() { resume(); },
function( error ) {
const message = "Promise rejected " +
( !phase ? "during" : phase.replace( /Each$/, "" ) ) +
" \"" + test.testName + "\": " +
( ( error && error.message ) || error );
test.pushFailure( message, extractStacktrace( error, 0 ) );

// Else next test will carry the responsibility
saveGlobal();

// Unblock
internalRecover( test );
}
);
const reject = function( error ) {
const message = "Promise rejected " +
( !phase ? "during" : phase.replace( /Each$/, "" ) ) +
" \"" + test.testName + "\": " +
( ( error && error.message ) || error );
test.pushFailure( message, extractStacktrace( error, 0 ) );

// Else next test will carry the responsibility
saveGlobal();

// Unblock
internalRecover( test );
};
then.call( promise, resolve, reject );
}
}
}
Expand Down Expand Up @@ -806,7 +804,6 @@ function internalStart( test ) {
"Invalid value on test.semaphore",
sourceFromStacktrace( 2 )
);
return;
}

// Don't start until equal number of stop-calls
Expand All @@ -822,7 +819,6 @@ function internalStart( test ) {
"Tried to restart test while already started (test's semaphore was 0 already)",
sourceFromStacktrace( 2 )
);
return;
}

// Add a slight delay to allow more assertions etc.
Expand Down
4 changes: 4 additions & 0 deletions test/cli/fixtures/assert-expect/failing-expect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
QUnit.test( "failing test", assert => {
assert.expect( 2 );
assert.true( true );
} );
2 changes: 2 additions & 0 deletions test/cli/fixtures/assert-expect/no-assertions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
QUnit.test( "test with no assertions", () => {
} );
5 changes: 5 additions & 0 deletions test/cli/fixtures/assert-expect/require-expects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
QUnit.config.requireExpects = true;

QUnit.test( "passing test", assert => {
assert.true( true );
} );
19 changes: 19 additions & 0 deletions test/cli/fixtures/config-module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
QUnit.config.module = "MODULE b";

QUnit.module( "Module A", () => {
QUnit.test( "Test A", assert => {
assert.true( false ); // fail if hit
} );
} );

QUnit.module( "Module B", () => {
QUnit.test( "Test B", assert => {
assert.true( true );
} );
} );

QUnit.module( "Module C", () => {
QUnit.test( "Test C", assert => {
assert.true( false ); // fail if hit
} );
} );
9 changes: 9 additions & 0 deletions test/cli/fixtures/config-testTimeout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
process.on( "unhandledRejection", ( reason ) => {
console.log( "Unhandled Rejection:", reason );
} );

QUnit.config.testTimeout = 10;

QUnit.test( "slow", () => {
return new Promise( resolve => setTimeout( resolve, 20 ) );
} );
2 changes: 1 addition & 1 deletion test/cli/fixtures/done-after-timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ QUnit.test( "times out before scheduled done is called", assert => {
assert.timeout( 10 );
const done = assert.async();
assert.true( true );
setTimeout( done, 20 );
setTimeout( done, 100 );
} );
26 changes: 26 additions & 0 deletions test/cli/fixtures/expected/tap-outputs.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,32 @@ ok 1 module providing hooks > module not providing hooks > has a test
# todo 0
# fail 0`,

"qunit config-module.js":
`TAP version 13
ok 1 Module B > Test B
1..1
# pass 1
# skip 0
# todo 0
# fail 0`,

"qunit config-testTimeout.js":
`TAP version 13
not ok 1 slow
---
message: Test took longer than 10ms; test timed out.
severity: failed
actual : null
expected: undefined
stack: |
at internal
...
1..1
# pass 0
# skip 0
# todo 0
# fail 1`,

"qunit done-after-timeout.js":
`TAP version 13
not ok 1 times out before scheduled done is called
Expand Down
8 changes: 8 additions & 0 deletions test/cli/fixtures/hard-error-in-hook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
QUnit.module( "contains a hard error in hook", hooks => {
hooks.before( () => {
throw new Error( "expected error thrown in hook" );
} );
QUnit.test( "contains a hard error", assert => {
assert.true( true );
} );
} );
8 changes: 8 additions & 0 deletions test/cli/fixtures/hard-error-in-test-with-no-async-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
QUnit.test( "contains a hard error after using `assert.async`", assert => {
assert.async();
assert.true( true );
throw new Error( "expected error thrown in test" );

// the "done" callback from `assert.async` should be called later,
// but the hard-error prevents the test from reaching that
} );
6 changes: 6 additions & 0 deletions test/cli/fixtures/noglobals/add-global.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
QUnit.config.noglobals = true;

QUnit.test( "adds global var", assert => {
global.dummyGlobal = "hello"; // eslint-disable-line no-undef
assert.true( true );
} );
6 changes: 6 additions & 0 deletions test/cli/fixtures/noglobals/ignored.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
QUnit.config.noglobals = true;

QUnit.test( "adds global var", assert => {
global[ "qunit-test-output-dummy" ] = "hello"; // eslint-disable-line no-undef
assert.true( true );
} );
8 changes: 8 additions & 0 deletions test/cli/fixtures/noglobals/remove-global.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
QUnit.config.noglobals = true;

global.dummyGlobal = "hello"; // eslint-disable-line no-undef

QUnit.test( "deletes global var", assert => {
delete global.dummyGlobal;
assert.true( true );
} );
18 changes: 18 additions & 0 deletions test/cli/fixtures/notrycatch/returns-rejection-in-hook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"use strict";

process.on( "unhandledRejection", ( reason ) => {
console.log( "Unhandled Rejection:", reason );
} );

QUnit.config.notrycatch = true;

QUnit.module( "notrycatch", function( hooks ) {

hooks.beforeEach( () => {
return Promise.reject( "bad things happen sometimes" );
} );

QUnit.test( "passing test", assert => {
assert.true( true );
} );
} );
5 changes: 5 additions & 0 deletions test/cli/fixtures/semaphore/nan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
QUnit.test( "semaphore is set to NaN", assert => {
assert.test.semaphore = "not a number";
assert.async();
return Promise.resolve();
} );
4 changes: 4 additions & 0 deletions test/cli/fixtures/semaphore/restart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
QUnit.test( "tries to 'restart' the test", assert => {
assert.test.semaphore = -1;
return Promise.resolve();
} );
2 changes: 1 addition & 1 deletion test/cli/helpers/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function normalize( actual ) {
// Convert "at foo (/min.js:1)\n -> /src.js:2" to "at foo (/src.js:2)"
.replace( /\b(at [^(]+\s\()[^)]+(\))\n\s+-> ([^\n]+)/g, "$1$3$2" )

.replace( / at .+\([^/)][^)]*\)/g, " at internal" )
.replace( / {2}at .+\([^/)][^)]*\)/g, " at internal" )

// merge successive lines after initial frame
.replace( /(\n\s+at internal)+/g, "$1" )
Expand Down
Loading

0 comments on commit dfdfd43

Please sign in to comment.