Mottie
Mottie

Reputation: 86453

Skipping a test in Qunit

I just found qHint, a method to integrate jsHint testing into Qunit... but it doesn't work locally (I don't mean localhost) except in Firefox.

So I wanted to add a "warning" or "notice", NOT a test failure, showing that the test was skipped:

// do unit test if not local or local and running Firefox
t = QUnit.isLocal;
if (!t || (t && /Firefox/.test(navigator.userAgent))) {
    jsHintTest('JSHint core check', 'js/myplugin.js');
} else {
    test('JSHint core check (skipped)', function(){
        ok( true, 'check not done locally' );
    });
}

I would just like to make it more obvious that a test was skipped, is this possible?


Update: Thanks to Odi for the answer!, but I had to make a slight modification to make the code work in QUnit v1.11.0pre:

QUnit.testSkip = function( testName, callback ) {
    QUnit.test(testName + ' (SKIPPED)', function() {
        if (typeof callback === "function") {
            callback();
        }
        var li = document.getElementById(QUnit.config.current.id);
        QUnit.done(function() {
            li.style.background = '#FFFF99';
        });
    });
};
testSkip = QUnit.testSkip;

Upvotes: 12

Views: 3160

Answers (2)

munsellj
munsellj

Reputation: 1597

For anyone who may have glazed over the comments, Mottie's comment on the question points out that Qunit now has a skip() function. Just replace any call to test() with skip() to skip that test.

Upvotes: 2

Odi
Odi

Reputation: 6916

I had the same requirement and I simply defined a new kind of test() that I called testSkip().

This test method simply replaces your test function and changes the name to <test name> (SKIPPED). After that the test is considered passed by QUnit.

To further indicate that this is a skipped test, I added a callback function to QUnit.done for each skipped test to change the color of the test in the HTML output to yellow. These callbacks are executed when the test suite is done. Setting the value directly does not work, because QUnit applies the styles for passed/failed tests at the end of the run.

QUnit.testSkip = function() {
   QUnit.test(arguments[0] + ' (SKIPPED)', function() {
       QUnit.expect(0);//dont expect any tests
       var li = document.getElementById(QUnit.config.current.id);
       QUnit.done(function() {
           li.style.background = '#FFFF99';
       });
   });
};
testSkip = QUnit.testSkip;

Then you can use testSkip() instead of test() for skipped tests.

For my test suite the result looks like that: Skipped tests in QUnit

Upvotes: 17

Related Questions