Skip to content

Commit e04ec8a

Browse files
committed
Docs: Add ESM dynamic import example and integration test
Make explicit mention of the "Unexpected test after runEnd." warning to help in discovery through our search function, and general web search. Closes #1694.
1 parent 3356e7f commit e04ec8a

File tree

8 files changed

+82
-22
lines changed

8 files changed

+82
-22
lines changed

.eslintrc.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,18 @@
138138
"compat/compat": "off"
139139
}
140140
},
141+
{
142+
"files": ["test/dynamic-import/**"],
143+
"env": {
144+
"es2017": true
145+
},
146+
"parserOptions": {
147+
"ecmaVersion": 2020
148+
},
149+
"rules": {
150+
"compat/compat": "off"
151+
}
152+
},
141153
{
142154
"files": ["test/node/**"],
143155
"env": {

Gruntfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ module.exports = function (grunt) {
7575

7676
'test/amd.html',
7777
'test/autostart.html',
78+
'test/dynamic-import.html',
7879
'test/events-filters.html',
7980
'test/events-in-test.html',
8081
'test/headless.html',

docs/QUnit/start.md

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: page-api
33
title: QUnit.start()
4-
excerpt: Start an async test suite.
4+
excerpt: Start the test runner.
55
groups:
66
- main
77
- async
@@ -10,21 +10,10 @@ redirect_from:
1010
version_added: "1.0.0"
1111
---
1212

13-
`QUnit.start()` must be used to start a test run that has [`QUnit.config.autostart`](../config/autostart.md) set to `false`.
13+
`QUnit.start()`
1414

15-
<p class="note note--warning" markdown="1">Warning: This method was previously used to control async tests on text contexts along with `QUnit.stop`. For asynchronous tests, use [`assert.async`](../assert/async.md) instead.</p>
15+
Start the test runner manually, when [`QUnit.config.autostart`](../config/autostart.md) is `false`. For example, if you load test files with AMD, RequireJS, or ESM dynamic imports.
1616

17-
When your async test has multiple exit points, call `QUnit.start()` for the corresponding number of `QUnit.stop()` increments.
17+
Note: See [`QUnit.config.autostart`](../config/autostart.md) for detailed examples of how to use this.
1818

19-
## Examples
20-
21-
A test run that does not begin when the page is done loading. This example uses an Asynchronous Module Definition (AMD) loader-style `require` call.
22-
23-
```js
24-
QUnit.config.autostart = false;
25-
26-
require(
27-
['test/tests1.js', 'test/tests2.js'],
28-
QUnit.start
29-
);
30-
```
19+
<p class="note note--warning" markdown="1">**Warning**: Prior to QUnit 1.16, this method was used for resuming an async `QUnit.start` function, as complement to `QUnit.stop()`. To resume asynchronous tests, use [`assert.async()`](../assert/async.md) instead.</p>

docs/config/autostart.md

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
22
layout: page-api
33
title: QUnit.config.autostart
4-
excerpt: Start running the tests once all files have been loaded.
4+
excerpt: Control when the test run may start.
55
groups:
66
- config
77
version_added: "1.0.0"
88
---
99

10-
Start running the tests once all files have been loaded.
10+
Control when the test run may start, e.g. after asynchronously loading test files with RequireJS, AMD, ES6 dynamic imports, or other means.
1111

1212
<table>
1313
<tr>
@@ -20,13 +20,40 @@ Start running the tests once all files have been loaded.
2020
</tr>
2121
</table>
2222

23-
By default, QUnit starts running the tests when `load` event is triggered on the `window` (in the browser), or after all specified files have been imported (in the CLI).
23+
In the browser, QUnit by default waits for all `<script>` elements to finish loading (by means of the window `load` event). When using the QUnit CLI, it waits until the specified files are imported.
2424

25-
Set this property to `false` if you load your test files asynchronously through custom means (e.g. RequireJS). Remember to call `QUnit.start()` once you're ready for tests to begin running.
25+
Set this property to `false` to instruct QUnit to wait longer, allowing you to load test files asynchronously. Remember to call [`QUnit.start()`](../QUnit/start.md) once you're ready for tests to begin running.
2626

27-
## Example
27+
If you asynchronously load test files _without_ disabling autostart, you may encounter this warning:
2828

29-
Disable autostart when loading tests asynchronously, such as when using RequireJS:
29+
<p class="note note--warning" markdown="1">**warning**: Unexpected test after runEnd.</p>
30+
31+
## Examples
32+
33+
### ESM Dynamic imports
34+
35+
This example uses the [import()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import) operator to dynamically load ECMAScript module (ESM) files.
36+
37+
```html
38+
<script src="../lib/qunit.js"></script>
39+
<script type="module" src="tests.js"></script>
40+
```
41+
42+
```js
43+
// tests.js
44+
QUnit.config.autostart = false;
45+
46+
Promise.all([
47+
import('./foo.js'),
48+
import('./bar.js')
49+
]).then(function () {
50+
QUnit.start();
51+
});
52+
```
53+
54+
### Loading with RequireJS
55+
56+
This example uses [RequireJS](https://requirejs.org/) to call a "require" function as defined by the [AMD specification](https://github.com/amdjs/amdjs-api/blob/master/require.md) (Asynchronous Module Definition).
3057

3158
```js
3259
QUnit.config.autostart = false;

test/dynamic-import.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>QUnit</title>
6+
<link rel="stylesheet" href="../qunit/qunit.css">
7+
</head>
8+
<body>
9+
<div id="qunit"></div>
10+
<script src="../qunit/qunit.js"></script>
11+
<script type="module" src="dynamic-import/index.js"></script>
12+
</body>
13+
</html>

test/dynamic-import/bar.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
QUnit.module('bar', function () {
2+
QUnit.test('example', function (assert) {
3+
assert.true(true);
4+
});
5+
});

test/dynamic-import/foo.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
QUnit.module('foo', function () {
2+
QUnit.test('example', function (assert) {
3+
assert.true(true);
4+
});
5+
});

test/dynamic-import/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
QUnit.config.autostart = false;
2+
3+
Promise.all([
4+
import('./foo.js'),
5+
import('./bar.js')
6+
]).then(function () {
7+
QUnit.start();
8+
});

0 commit comments

Comments
 (0)