QUnit.test.if()
version added: 2.22.0.
Description
QUnit.test.if( name, condition, callback )
Add a test that only runs if a condition is true.
| parameter | description |
|---|---|
name (string) |
Title of unit being tested |
condition (string) |
Expression to decide if the test should run |
callback (function) |
Function that performs the test |
If the condition is true, this is equivalent to calling QUnit.test().
If the conditional is false, this is equivalent to calling QUnit.test.skip(), and test will not run. Instead, it will be listed in the results as a “skipped” test.
As a codebase becomes bigger, you may need to conditionally skip an entire group of tests. You can use QUnit.module.if() to recursively skip all tests in a module based on a given requirement.
Examples
Skip a test
QUnit.module('MyApp');
// Skip if executed without a DOM
QUnit.test.if('render', typeof document !== 'undefined', function (assert) {
assert.strictEqual(MyApp.render(), '<p>Hello world!</p>');
});
This is equivalent to:
QUnit.module('MyApp');
if (typeof document !== 'undefined') {
QUnit.test('render', function (assert) {
assert.strictEqual(MyApp.render(), '<p>Hello world!</p>');
});
} else {
QUnit.test.skip('render');
}
Use QUnit.module.if(), to conditionally skip all tests in a given module.
QUnit.module.if('MyApp', typeof document !== 'undefined');
QUnit.test('render', function (assert) {
assert.strictEqual(MyApp.render(), '<p>Hello world!</p>');
});
Legacy idiom
Prior to QUnit 2.22, the following shortcuts were sometimes used. This may be replaced by QUnit.test.if().
(typeof document !== 'undefined' ? QUnit.test : QUnit.skip)('example', function (assert) {
assert.true(true);
});
QUnit[typeof document !== 'undefined' ? 'test' : 'skip']('example', function (assert) {
assert.strue(true);
});