Skip to content

Commit

Permalink
Core: keeping count of tests not ran to ensure moduleDone is called
Browse files Browse the repository at this point in the history
  • Loading branch information
step2yeung committed Nov 22, 2019
1 parent e678729 commit 7afb00a
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 8 deletions.
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ module.exports = function( grunt ) {
"test/reorderError2.html",
"test/callbacks.html",
"test/callbacks-promises.html",
"test/callbacks-filters.html",
"test/events.html",
"test/events-in-test.html",
"test/logs.html",
Expand Down
1 change: 1 addition & 0 deletions src/core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const config = {
childModules: [],
testsRun: 0,
unskippedTestsRun: 0,
testsNotRan: 0,
hooks: {
before: [],
beforeEach: [],
Expand Down
1 change: 1 addition & 0 deletions src/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function createModule( name, testEnvironment, modifiers ) {
tests: [],
moduleId: generateHash( moduleName ),
testsRun: 0,
testsNotRan: 0,
unskippedTestsRun: 0,
childModules: [],
suiteReport: new SuiteReport( name, parentSuite ),
Expand Down
31 changes: 23 additions & 8 deletions src/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ Test.prototype = {
}
}

notifyTestsRan( module, skipped );
updateTestsRan( module, skipped );

// Store result when possible
if ( storage ) {
Expand Down Expand Up @@ -344,13 +344,15 @@ Test.prototype = {
// generating stack trace is expensive, so using a getter will help defer this until we need it
get source() { return test.stack; }
} ).then( function() {
if ( module.testsRun === numberOfTests( module ) ) {
if ( ( module.testsRun + module.testsNotRan ) === numberOfTests( module ) ) {
const completedModules = [ module ];

// Check if the parent modules, iteratively, are done. If that the case,
// we emit the `suiteEnd` event and trigger `moduleDone` callback.
let parent = module.parentModule;
while ( parent && parent.testsRun === numberOfTests( parent ) ) {
while ( parent &&
( parent.testsRun + parent.testsNotRan ) === numberOfTests( parent )
) {
completedModules.push( parent );
parent = parent.parentModule;
}
Expand Down Expand Up @@ -394,6 +396,7 @@ Test.prototype = {
const test = this;

if ( !this.valid() ) {
updateTestsNotRan( this.module );
return;
}

Expand Down Expand Up @@ -551,10 +554,7 @@ Test.prototype = {
},

valid: function() {
var filter = config.filter,
regexFilter = /^(!?)\/([\w\W]*)\/(i?$)/.exec( filter ),
module = config.module && config.module.toLowerCase(),
fullName = ( this.module.name + ": " + this.testName );
var module = config.module && config.module.toLowerCase();

function moduleChainNameMatch( testModule ) {
var testModuleName = testModule.name ? testModule.name.toLowerCase() : null;
Expand Down Expand Up @@ -593,6 +593,14 @@ Test.prototype = {
return false;
}

return this.filterMatch();
},

filterMatch: function() {
var filter = config.filter,
regexFilter = /^(!?)\/([\w\W]*)\/(i?$)/.exec( filter ),
fullName = ( this.module.name + ": " + this.testName );

if ( !filter ) {
return true;
}
Expand Down Expand Up @@ -865,7 +873,7 @@ function numberOfUnskippedTests( module ) {
return collectTests( module ).filter( test => !test.skip ).length;
}

function notifyTestsRan( module, skipped ) {
function updateTestsRan( module, skipped ) {
module.testsRun++;
if ( !skipped ) {
module.unskippedTestsRun++;
Expand All @@ -877,3 +885,10 @@ function notifyTestsRan( module, skipped ) {
}
}
}

function updateTestsNotRan( module ) {
module.testsNotRan++;
while ( ( module = module.parentModule ) ) {
module.testsNotRan++;
}
}
14 changes: 14 additions & 0 deletions test/callbacks-filters.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>QUnit Callbacks Filters Test Suite</title>
<link rel="stylesheet" href="../dist/qunit.css">
<script src="../dist/qunit.js"></script>
<script src="callbacks-filters.js"></script>
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
</body>
</html>
157 changes: 157 additions & 0 deletions test/callbacks-filters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
var invokedHooks = [];
var done = false;

function callback( name ) {
return function() {
if ( done ) {
return;
}

invokedHooks.push( name );
};
}

QUnit.begin( callback( "begin" ) );
QUnit.moduleStart( callback( "moduleStart" ) );
QUnit.testStart( callback( "testStart" ) );
QUnit.testDone( callback( "testDone" ) );
QUnit.moduleDone( callback( "moduleDone" ) );
QUnit.done( callback( "done" ) );

QUnit.done( function() {
if ( done ) {
return;
}

done = true;

QUnit.test( "verify callback order", function( assert ) {
assert.deepEqual( invokedHooks, [
"begin",
"moduleStart",
"testStart",
"testDone",
"testStart",
"testDone",
"moduleDone",
"moduleStart",
"testStart",
"testDone",
"moduleDone",
"moduleStart",
"testStart",
"testDone",
"moduleDone",
"moduleStart",
"testStart",
"testDone",
"moduleStart",
"testStart",
"testDone",
"moduleDone",
"moduleDone",
"moduleStart",
"moduleStart",
"testStart",
"testDone",
"moduleDone",
"moduleDone",
"moduleStart",
"testStart",
"testDone",
"moduleDone",
"done"
] );
} );
} );

QUnit.config.moduleId = [ "1cf055b9" ];

// match for module Id
QUnit.module( "module1", function() {
QUnit.test( "test should run 1.1", function( assert ) {
assert.ok( true );
} );

QUnit.test( "test should run 1.2", function( assert ) {
assert.ok( true );
} );
} );

// no match module Id
QUnit.module( "module2", function() {
QUnit.test( "test should NOT run 2.1", function( assert ) {
assert.ok( false );
} );

QUnit.test( "test should NOT run 2.2", function( assert ) {
assert.ok( false );
} );
} );

QUnit.config.moduleId = [];
QUnit.config.testId = [ "3b3c4e75" ];

QUnit.module( "module3", function() {

// match test Id
QUnit.test( "ABCtest should run 3.1", function( assert ) {
assert.ok( true );
} );

QUnit.test( "test should NOT run 3.2", function( assert ) {
assert.ok( false );
} );
} );

QUnit.config.testId = [];
QUnit.config.filter = "!.2";

QUnit.module( "module4", function() {

// match string filter
QUnit.test( "test should run 4.1", function( assert ) {
assert.ok( true );
} );

QUnit.test( "test should NOT run 4.2", function( assert ) {
assert.ok( false );
} );
} );

// Make sure nest module scenarios are correct
QUnit.module( "module5", function() {
QUnit.test( "test", function( assert ) {
assert.ok( true );
} );

QUnit.module( "nestedModule5A", function() {
QUnit.test( "test should run 5.A.1", function( assert ) {
assert.ok( true );
} );
} );
} );

QUnit.module( "module6", function() {
QUnit.test( "test should NOT run 6.2", function( assert ) {
assert.ok( true );
} );

QUnit.module( "nestedModule6A", function() {
QUnit.test( "test should run 6.A.1", function( assert ) {
assert.ok( true );
} );
} );
} );

QUnit.module( "module7", function() {
QUnit.test( "test should run 7.1", function( assert ) {
assert.ok( true );
} );

QUnit.module( "nestedModule7A", function() {
QUnit.test( "test should NOT run 7.A.2", function( assert ) {
assert.ok( true );
} );
} );
} );

0 comments on commit 7afb00a

Please sign in to comment.