Skip to content

Commit 4feb059

Browse files
committed
Docs: Add QUnit.test.if example for in-browser feature test
Ref #1815.
1 parent e8722fc commit 4feb059

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

docs/api/QUnit/module.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ QUnit.module.skip('Robot', (hooks) => {
256256
});
257257
```
258258

259-
Use `QUnit.module.if()`, to conditionally skip an entire module. For example, if these tests are only meant to run in certain environments, operating systems, or when certain optional dependencies are installed.
259+
Use `QUnit.module.if()`, to conditionally skip an entire module. For example, if these tests are only meant to run in certain environments, operating systems, or when certain optional dependencies are installed. See also [`QUnit.test.if()`](./test.if.md).
260260

261261
```js
262262
QUnit.module.if('MyApp', typeof document !== 'undefined', (hooks) => {

docs/api/QUnit/test.if.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,37 @@ If the condition is true, this is equivalent to calling [`QUnit.test()`](./test.
2323

2424
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.
2525

26-
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 requirement.
26+
Use cases:
27+
* Skip tests for a feature not supported in an older browser, when cross-browser testing in CI.
28+
* Skip tests for code that relies on an optional dependency.
29+
* Skip tests for an optional integration on one operating system, when testing a cross-platform application on Linux, macOS, and Windows.
30+
31+
You can use [`QUnit.module.if()`](./module.md) to skip several tests or nested modules at once, without repeating the same condition.
2732

2833
## Examples
2934

35+
### Feature test
36+
37+
This is an example that automatically skips a test in environments where [WebGL](https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Tutorial) is unsupported. For example, if you do cross-browser testing in CI and support older browsers in your project, you can use this to cover optional features for newer browsers that are expected to fail in older browsers.
38+
39+
```js
40+
QUnit.module('MyApp');
41+
42+
QUnit.test.if('drawCanvas [red square]', typeof WebGLRenderingContext !== 'undefined', function (assert) {
43+
const canvas = MyApp.drawCanvas();
44+
assert.pixelEqual(canvas, 10, 10, 255, 0, 0);
45+
assert.pixelEqual(canvas, 20, 20, 255, 0, 0);
46+
});
47+
```
48+
3049
### Skip a test
3150

51+
This example is for a library that targets both browsers and Node.js, where this test is for a feature that is only meant to work in browsers, and naturally skipped when the test suite is run in Node.js.
52+
3253
```js
3354
QUnit.module('MyApp');
3455

35-
// Skip if executed without a DOM
56+
// Browser-only test, skipped if executed without a DOM
3657
QUnit.test.if('render', typeof document !== 'undefined', function (assert) {
3758
assert.strictEqual(MyApp.render(), '<p>Hello world!</p>');
3859
});

0 commit comments

Comments
 (0)