QUnit.config.autostart
version added: 1.0.0.
Description
Control when the test run may start, e.g. after asynchronously loading test files with RequireJS, AMD, ES6 dynamic imports, or other means.
type | boolean |
---|---|
default | true |
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.
Set this property to false
to instruct QUnit to wait longer, allowing you to load test files asynchronously. Remember to call QUnit.start()
once you’re ready for tests to begin running.
Examples
Error: Unexpected test after runEnd
If tests are new tests defined after QUnit has finished its run, you may encounter this error:
Error: Unexpected test after runEnd.
If you load test files asynchronously, make sure to disable autostart and call QUnit.start()
accordingly.
If you encounter this error unrelated to autostart, it might be that you’re dynamically registering a new QUnit.test from inside a hook or event callback towards the end of the test run, such as hooks.after()
or QUnit.done()
. It is recommended to define dynamic tests via QUnit.begin()
instead. (#1663)
To report global errors from a plugin or other integration layer, consider calling QUnit.onUncaughtException() instead.
ESM Dynamic imports
This example uses the import() operator to dynamically load ECMAScript module (ESM) files.
<script src="../lib/qunit.js"></script>
<script type="module" src="tests.js"></script>
// tests.js
QUnit.config.autostart = false;
Promise.all([
import('./foo.js'),
import('./bar.js')
]).then(function () {
QUnit.start();
});
Loading with RequireJS
This example uses RequireJS to load your test files through the require()
function (as defined in the AMD specification).
It is recommended to load QUnit itself before RequireJS. See also RequireJS wiki.
<!DOCTYPE html>
<meta charset="utf-8">
<title>QUnit</title>
<link rel="stylesheet" href="./lib/qunit.css">
<body>
<div id="qunit"></div>
<script src="../lib/qunit.js"></script>
<script src="../lib/requirejs/require.js"></script>
<script src="tests.js"></script>
</body>
// tests.js
QUnit.config.autostart = false;
require(
[
'tests/testModule1',
'tests/testModule2'
],
function () {
QUnit.start();
}
);