Skip to content

Commit

Permalink
Core: Implements QUnit.skip
Browse files Browse the repository at this point in the history
Also logs skipped tests on testDone callback that are handled on
the html reporter

Fixes #637
Fixes #434
  • Loading branch information
leobalter committed Sep 8, 2014
1 parent d04101c commit dea89c1
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 51 deletions.
3 changes: 3 additions & 0 deletions reporter/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,9 @@ QUnit.testDone(function( details ) {
time.innerHTML = details.runtime + " ms";

testItem.className = bad ? "fail" : "pass";
if ( details.skipped ) {
addClass( testItem, "skipped" );
}

testItem.insertBefore( time, assertList );
});
Expand Down
59 changes: 10 additions & 49 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,17 @@ QUnit = {
testName: testName,
expected: expected,
async: async,
callback: callback,
module: config.currentModule,
moduleTestEnvironment: config.currentModuleTestEnvironment,
stack: sourceFromStacktrace( 2 )
callback: callback
});

if ( !validTest( test ) ) {
return;
}
test.queue();
},

skip: function( testName ) {
var test = new Test({
testName: testName,
skip: true
});

test.queue();
},
Expand Down Expand Up @@ -447,7 +449,7 @@ window.onerror = function( error, filePath, linerNr ) {
} else {
QUnit.test( "global failure", extend(function() {
QUnit.pushFailure( error, filePath + ":" + linerNr );
}, { validTest: validTest } ) );
}, { validTest: true } ) );
}
return false;
}
Expand Down Expand Up @@ -481,47 +483,6 @@ function done() {
});
}

/** @return Boolean: true if this test should be ran */
function validTest( test ) {
var include,
filter = config.filter && config.filter.toLowerCase(),
module = config.module && config.module.toLowerCase(),
fullName = ( test.module + ": " + test.testName ).toLowerCase();

// Internally-generated tests are always valid
if ( test.callback && test.callback.validTest === validTest ) {
delete test.callback.validTest;
return true;
}

if ( config.testNumber.length > 0 ) {
if ( inArray( test.testNumber, config.testNumber ) < 0 ) {
return false;
}
}

if ( module && ( !test.module || test.module.toLowerCase() !== module ) ) {
return false;
}

if ( !filter ) {
return true;
}

include = filter.charAt( 0 ) !== "!";
if ( !include ) {
filter = filter.slice( 1 );
}

// If the filter matches, we need to honour include
if ( fullName.indexOf( filter ) !== -1 ) {
return include;
}

// Otherwise, do the opposite
return !include;
}

// Doesn't support IE6 to IE9
// See also https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error/Stack
function extractStacktrace( e, offset ) {
Expand Down
5 changes: 5 additions & 0 deletions src/qunit.css
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@

#qunit-banner.qunit-fail { background-color: #EE5757; }

/*** Skipped tests */

#qunit-tests .skipped {
background-color: #EBECE9;
}

/** Result */

Expand Down
63 changes: 62 additions & 1 deletion src/test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
function Test( settings ) {
extend( this, settings );
this.assert = new Assert( this );
this.assertions = [];
this.testNumber = ++Test.count;
this.module = config.currentModule;
this.moduleTestEnvironment = config.currentModuleTestEnvironment;
this.stack = sourceFromStacktrace( 3 );

if ( settings.skip ) {
this.callback = function() {};
this.async = false;
this.expected = 0;
} else {
this.assert = new Assert( this );
}
}

Test.count = 0;
Expand Down Expand Up @@ -102,6 +112,11 @@ Test.prototype = {
hooks: function( handler ) {
var hooks = [];

// hooks are also ignored on skipped tests
if ( this.skip ) {
return hooks;
}

if ( QUnit.objectType( config[ handler ] ) === "function" ) {
hooks.push( this.queueHook( config[ handler ], handler ) );
}
Expand Down Expand Up @@ -139,6 +154,7 @@ Test.prototype = {
runLoggingCallbacks( "testDone", {
name: this.testName,
module: this.module,
skipped: !!this.skip,
failed: bad,
passed: this.assertions.length - bad,
total: this.assertions.length,
Expand All @@ -159,6 +175,10 @@ Test.prototype = {
var bad,
test = this;

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

function run() {

// each of these can by async
Expand Down Expand Up @@ -250,6 +270,47 @@ Test.prototype = {
result: false,
message: message
});
},

/** @return Boolean: true if this test should be ran */
valid: function() {
var include,
filter = config.filter && config.filter.toLowerCase(),
module = config.module && config.module.toLowerCase(),
fullName = ( this.module + ": " + this.testName ).toLowerCase();

// Internally-generated tests are always valid
if ( this.callback && this.callback.validTest ) {
delete this.callback.validTest;
return true;
}

if ( config.testNumber.length > 0 ) {
if ( inArray( this.testNumber, config.testNumber ) < 0 ) {
return false;
}
}

if ( module && ( !this.module || this.module.toLowerCase() !== module ) ) {
return false;
}

if ( !filter ) {
return true;
}

include = filter.charAt( 0 ) !== "!";
if ( !include ) {
filter = filter.slice( 1 );
}

// If the filter matches, we need to honour include
if ( fullName.indexOf( filter ) !== -1 ) {
return include;
}

// Otherwise, do the opposite
return !include;
}
};

Expand Down
3 changes: 2 additions & 1 deletion test/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ QUnit.test( "test2", function( assert ) {
failed: 0,
passed: 17,
total: 17,
testNumber: 1
testNumber: 1,
skipped: false
}, "testDone context" );
assert.deepEqual( testContext, {
module: "logs1",
Expand Down
22 changes: 22 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,3 +438,25 @@ QUnit.test( "mod2", function( assert ) {
assert.mod2( 2, 0, "2 % 2 == 0" );
assert.mod2( 3, 1, "3 % 2 == 1" );
});

QUnit.module( "QUnit.skip", {
beforeEach: function( assert ) {

// skip test hooks for skipped tests
assert.ok( false, "skipped function" );
throw "Error";
},
afterEach: function( assert ) {
assert.ok( false, "skipped function" );
throw "Error";
}
});

QUnit.skip( "test blocks are skipped", function( assert ) {

// this test callback won't run, even with broken code
assert.expect( 1000 );
throw "Error";
});

QUnit.skip( "no function" );

0 comments on commit dea89c1

Please sign in to comment.