-
Notifications
You must be signed in to change notification settings - Fork 779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Core: Add QUnit.test.if()
and QUnit.module.if()
#1772
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 be listed in the results as a "skipped" test. | ||
Krinkle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These many parameters, including boolean ones, don't read that well. But maybe it's not a big issue in practice since you'd normally see a condition here, not a direct boolean value. |
||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In jQuery, we arrived at the following pattern:
QUnit.skip
.if
to avoid showing the test as skipped - and producing noise - in modern browsers.I'm not sure if you'd like to do anything with this knowledge but I thought it might be useful to know that when thinking about the design.