Skip to content

Commit

Permalink
Core: Implements QUnit.only
Browse files Browse the repository at this point in the history
Fixes #496
Closes #875
  • Loading branch information
ebenoist authored and leobalter committed Oct 25, 2015
1 parent 2b123d4 commit efe0303
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Gruntfile.js
Expand Up @@ -106,7 +106,8 @@ grunt.initConfig({
"test/reporter-html/index.html",
"test/reporter-html/legacy-markup.html",
"test/reporter-html/no-qunit-element.html",
"test/reporter-html/single-testid.html"
"test/reporter-html/single-testid.html",
"test/only.html"
]
},
coveralls: {
Expand Down
2 changes: 2 additions & 0 deletions src/core.js
Expand Up @@ -85,6 +85,8 @@ extend( QUnit, {

skip: skip,

only: only,

// DEPRECATED: The functionality of QUnit.start() will be altered in QUnit 2.0.
// In QUnit 2.0, invoking it will ONLY affect the `QUnit.config.autostart` blocking behavior.
start: function( count ) {
Expand Down
31 changes: 30 additions & 1 deletion src/test.js
@@ -1,3 +1,5 @@
var focused = false;

function Test( settings ) {
var i, l;

Expand Down Expand Up @@ -246,7 +248,6 @@ Test.prototype = {
},

test.hooks( "beforeEach" ),

function() {
test.run();
},
Expand Down Expand Up @@ -545,6 +546,8 @@ function asyncTest( testName, expected, callback ) {

// Will be exposed as QUnit.test
function test( testName, expected, callback, async ) {
if ( focused ) { return; }

var newTest;

if ( arguments.length === 2 ) {
Expand All @@ -564,10 +567,36 @@ function test( testName, expected, callback, async ) {

// Will be exposed as QUnit.skip
function skip( testName ) {
if ( focused ) { return; }

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

test.queue();
}

// Will be exposed as QUnit.only
function only( testName, expected, callback, async ) {
var newTest;

if ( focused ) { return; }

QUnit.config.queue.length = 0;
focused = true;

if ( arguments.length === 2 ) {
callback = expected;
expected = null;
}

newTest = new Test({
testName: testName,
expected: expected,
async: async,
callback: callback
});

newTest.queue();
}
13 changes: 13 additions & 0 deletions test/only.html
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>QUnit Only Test Suite</title>
<link rel="stylesheet" href="../dist/qunit.css">
<script src="../dist/qunit.js"></script>
<script src="only.js"></script>
</head>
<body>
<div id="qunit"></div>
</body>
</html>
17 changes: 17 additions & 0 deletions test/only.js
@@ -0,0 +1,17 @@
QUnit.module( "QUnit.only" );

QUnit.test( "implicitly skipped test", function( assert ) {
assert.ok( false, "test should be skipped" );
});

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

QUnit.test( "another implicitly skipped test", function( assert ) {
assert.ok( false, "test should be skipped" );
});

QUnit.only( "ignore the subsequent calls to only", function( assert ) {
assert.ok( false, "this test should be skipped" );
});

0 comments on commit efe0303

Please sign in to comment.