-
Notifications
You must be signed in to change notification settings - Fork 780
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Core: Add
QUnit.test.if()
and QUnit.module.if()
Closes #1772. Co-authored-by: Steve McClure <[email protected]>
- Loading branch information
1 parent
894d3cc
commit a165ab8
Showing
13 changed files
with
152 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
--- | ||
layout: page-api | ||
title: QUnit.test.if() | ||
excerpt: Add a test that may be skipped. | ||
groups: | ||
- main | ||
redirect_from: | ||
- "/QUnit/test.if/" | ||
version_added: "unreleased" | ||
--- | ||
|
||
`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 be run | | ||
| `callback` (function) | Function that performs the test | | ||
|
||
If the condition is true, this is equivalent to calling [`QUnit.test()`](./test.md). | ||
|
||
If the conditional is false, this is equivalent to calling [`QUnit.test.skip()`](./test.skip.md), 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()`](./module.md) to recursively skip all tests in a module based on a given condition. | ||
|
||
## Examples | ||
|
||
```js | ||
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>'); | ||
}); | ||
``` | ||
|
||
```js | ||
QUnit.module.if('MyApp', typeof document !== 'undefined'); | ||
|
||
QUnit.test('render', function (assert) { | ||
assert.strictEqual(MyApp.render(), '<p>Hello world!</p>'); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
QUnit.test.if('skip me', false, function (assert) { | ||
assert.true(false); | ||
}); | ||
|
||
QUnit.test.if('keep me', true, function (assert) { | ||
assert.true(true); | ||
}); | ||
|
||
QUnit.test('regular', function (assert) { | ||
assert.true(true); | ||
}); | ||
|
||
QUnit.test.if.each('skip dataset', false, ['a', 'b'], function (assert, _data) { | ||
assert.true(false); | ||
}); | ||
|
||
QUnit.test.if.each('keep dataset', true, ['a', 'b'], function (assert, data) { | ||
assert.true(true); | ||
assert.equal(typeof data, 'string'); | ||
}); | ||
|
||
QUnit.module.if('skip group', false, function () { | ||
QUnit.test('skipper', function (assert) { | ||
assert.true(false); | ||
}); | ||
}); | ||
|
||
QUnit.module.if('keep group', true, function (hooks) { | ||
let list = []; | ||
hooks.beforeEach(function () { | ||
list.push('x'); | ||
}); | ||
QUnit.test('keeper', function (assert) { | ||
assert.true(true); | ||
assert.deepEqual(list, ['x']); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# name: no tests | ||
# command: ["qunit", "test-if.js"] | ||
|
||
TAP version 13 | ||
ok 1 # SKIP skip me | ||
ok 2 keep me | ||
ok 3 regular | ||
ok 4 # SKIP skip dataset [0] | ||
ok 5 # SKIP skip dataset [1] | ||
ok 6 keep dataset [0] | ||
ok 7 keep dataset [1] | ||
ok 8 # SKIP skip group > skipper | ||
ok 9 keep group > keeper | ||
1..9 | ||
# pass 5 | ||
# skip 4 | ||
# todo 0 | ||
# fail 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters