Skip to content

Commit

Permalink
Core: Decide "No tests" based on test count, not assert count
Browse files Browse the repository at this point in the history
If a test has no assertions, that is generally by itself another
unrelated error. But more importantly, it is configurable, and
also something that we support on a per-test basis via
assert.expect(0) for which there are several use cases such
as when the tests only purpose is not to throw, either from
your source code, or from another helper library (e.g. Sinon
assertion's throwing).

Previously, if your test suite composed only of such tests,
or if you selective run such test only, it wrongly caused
the test runner to report a global error for "No tests."

Fixes #1405.
Closes #1449.
  • Loading branch information
jayjayjpg authored and Krinkle committed Jun 22, 2020
1 parent 474a708 commit db78338
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ extend( QUnit, {

// Initialize the configuration options
extend( config, {
stats: { all: 0, bad: 0 },
stats: { all: 0, bad: 0, testCount: 0 },
started: 0,
updateRate: 1000,
autostart: true,
Expand Down
2 changes: 1 addition & 1 deletion src/core/processing-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function done() {
const runtime = now() - config.started;
const passed = config.stats.all - config.stats.bad;

if ( config.stats.all === 0 ) {
if ( config.stats.testCount === 0 ) {

if ( config.filter && config.filter.length ) {
throw new Error( `No tests matched the filter "${config.filter}".` );
Expand Down
1 change: 1 addition & 0 deletions src/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ Test.prototype = {
this.runtime = now() - this.started;

config.stats.all += this.assertions.length;
config.stats.testCount += 1;
module.stats.all += this.assertions.length;

for ( i = 0; i < this.assertions.length; i++ ) {
Expand Down
9 changes: 9 additions & 0 deletions test/cli/fixtures/expected/tap-outputs.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,15 @@ ok 2 timeout > second
# fail 1
`,

"qunit zero-assertions.js":
`TAP version 13
ok 1 Zero assertions > has a test
1..1
# pass 1
# skip 0
# todo 0
# fail 0`,

// in node 8, the stack trace includes 'at <anonymous>. But not in node 6 or 10.
"qunit qunit --filter 'no matches' test":
`TAP version 13
Expand Down
8 changes: 8 additions & 0 deletions test/cli/fixtures/zero-assertions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
QUnit.module( "Zero assertions", function() {
QUnit.test( "has a test", function( assert ) {
assert.expect( 0 );

// A test may expect zero assertions if its main purpose
// is to ensure there are no no run-time exceptions.
} );
} );
9 changes: 9 additions & 0 deletions test/cli/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ QUnit.module( "CLI Main", function() {
}
} ) );

QUnit.test( "allows running zero-assertion tests", co.wrap( function* ( assert ) {
const command = "qunit zero-assertions.js";
const execution = yield execute( command );

assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} ) );

if ( semver.gte( process.versions.node, "9.0.0" ) ) {
QUnit.test( "callbacks and hooks from modules are properly released for garbage collection", co.wrap( function* ( assert ) {
const command = "node --expose-gc --allow-natives-syntax ../../../bin/qunit.js memory-leak/*.js";
Expand Down

0 comments on commit db78338

Please sign in to comment.