Skip to content
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

Docs: add complete '--delay' example #4744

Merged
merged 1 commit into from
Sep 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -414,18 +414,27 @@ As of v8.0.0, [Root Hook Plugins] are the preferred mechanism for setting root h

> _WARNING: Delayed root suites are incompatible with [parallel mode](#parallel-tests)._

If you need to perform asynchronous operations before any of your suites are run, you may delay the root suite. Run `mocha` with the `--delay` flag. This will attach a special callback function, `run()`, to the global context:
If you need to perform asynchronous operations before any of your suites are run (e.g. for dynamically generating tests), you may delay the root suite. Run `mocha` with the `--delay` flag. This will attach a special callback function, `run()`, to the global context:

```js
setTimeout(function() {
// do some setup
const assert = require('assert');

const fn = async x => { return new Promise(
resolve => { setTimeout(resolve, 3000, 2 * x); }
)};

// instead of an IIFE, you can use 'setImmediate' or 'nextTick' or 'setTimeout'
(async function() {
const z = await fn(3);

describe('my suite', function() {
// ...
it(`expected value ${z}`, function() {
assert.equal(z, 6);
});
});

run();
}, 5000);
})();
```

## Pending Tests
Expand Down Expand Up @@ -734,7 +743,8 @@ describe('add()', function() {
});
```

With `top-level await` you can collect your test data in a dynamic and asynchronous way while the test file is being loaded:
With `top-level await` you can collect your test data in a dynamic and asynchronous way while the test file is being loaded.<br>
See also [`--delay`](#delayed-root-suite) for CommonJS modules without `top-level await`.

```js
// testfile.mjs
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
],
"browser": {
"./index.js": "./browser-entry.js",
"./lib/nodejs/growl.js": "./lib/browser/growl.js",
"./lib/nodejs/growl.js": "./lib/browser/growl.js",
"fs": false,
"path": false,
"supports-color": false,
Expand Down