Skipping a test in Qunit
Asked Answered
P

2

12

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;
Polynomial answered 6/12, 2012 at 16:31 Comment(2)
QUnit has added a skip() method in v1.16!Polynomial
https://api.qunitjs.com/QUnit/skipKristin
M
17

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

Microscopic answered 6/12, 2012 at 19:47 Comment(4)
I had to make a slight modification to get it to work in QUnit v1.11.0pre (posted above). Thanks!Polynomial
You'll need to add ok(true); inside the modified test or QUnit v1.2 will complain there are zero assertions in the test.Neslund
@FrancoisVanderseypen There are zero asserts in the test, wouldn't it be better to use expect(0); instead?Dissertate
+1, it would still be good to skip running any setup/teardown code for skipped tests. Any clue on how to achieve that?Jorgenson
I
2

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.

Idempotent answered 18/5, 2016 at 18:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.