Skip to content
Open
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
35 changes: 35 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -2762,6 +2762,38 @@ changes:
Configures the test runner to only execute top level tests that have the `only`
option set. This flag is not necessary when test isolation is disabled.

### `--test-random-seed`

<!-- YAML
added: REPLACEME
-->

Set the seed used to randomize test execution order. This applies to both test
file execution order and queued tests within each file. Providing this flag
enables randomization implicitly, even without `--test-randomize`.

The value must be an integer between `0` and `4294967295`.

This flag cannot be used with `--watch` or `--test-rerun-failures`.

### `--test-randomize`
Copy link
Member

@MoLow MoLow Feb 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this feature should either be dissallowed when using --test-rerun-failures, or we should somehow save the seed in the rerun state so a rerun will be deterministic.


<!-- YAML
added: REPLACEME
-->

Randomize test execution order. This applies to both test file execution order
and queued tests within each file. This can help detect tests that rely on
shared state or execution order.

The seed used for randomization is printed in the test summary and can be
reused with `--test-random-seed`.

For detailed behavior and examples, see
[randomizing tests execution order][].

This flag cannot be used with `--watch` or `--test-rerun-failures`.

### `--test-reporter`

<!-- YAML
Expand Down Expand Up @@ -3679,6 +3711,8 @@ one is included in the list below.
* `--test-isolation`
* `--test-name-pattern`
* `--test-only`
* `--test-random-seed`
* `--test-randomize`
* `--test-reporter-destination`
* `--test-reporter`
* `--test-rerun-failures`
Expand Down Expand Up @@ -4255,6 +4289,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
[libuv threadpool documentation]: https://docs.libuv.org/en/latest/threadpool.html
[module compile cache]: module.md#module-compile-cache
[preloading asynchronous module customization hooks]: module.md#registration-of-asynchronous-customization-hooks
[randomizing tests execution order]: test.md#randomizing-tests-execution-order
[remote code execution]: https://www.owasp.org/index.php/Code_Injection
[running tests from the command line]: test.md#running-tests-from-the-command-line
[scavenge garbage collector]: https://v8.dev/blog/orinoco-parallel-scavenger
Expand Down
99 changes: 99 additions & 0 deletions doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,94 @@ prevent shell expansion, which can reduce portability across systems.
node --test "**/*.test.js" "**/*.spec.js"
```

### Randomizing tests execution order

<!-- YAML
added: REPLACEME
-->

> Stability: 1.0 - Early development

The test runner can randomize execution order to help detect
order-dependent tests. When enabled, the runner randomizes both discovered
test files and queued tests within each file. Use `--test-randomize` to
enable this mode.

```bash
node --test --test-randomize
```

When randomization is enabled, the test runner prints the seed used for the run
as a diagnostic message:

```text
Randomized test order seed: 12345
```

Use `--test-random-seed=<number>` to replay the same randomized order
deterministically. Supplying `--test-random-seed` also enables randomization,
so `--test-randomize` is optional when a seed is provided:

```bash
node --test --test-randomize --test-random-seed=12345
```

In most test files, randomization works automatically. One important exception
is when subtests are awaited one by one. In that pattern, each subtest starts
only after the previous one finishes, so the runner keeps declaration order
instead of randomizing it.

Example: this runs sequentially and is **not** randomized.

```mjs
import test from 'node:test';

test('math', async (t) => {
for (const name of ['adds', 'subtracts', 'multiplies']) {
// Sequentially awaiting each subtest preserves declaration order.
await t.test(name, async () => {});
}
});
```

```cjs
const test = require('node:test');

test('math', async (t) => {
for (const name of ['adds', 'subtracts', 'multiplies']) {
// Sequentially awaiting each subtest preserves declaration order.
await t.test(name, async () => {});
}
});
```

Using suite-style APIs such as `describe()`/`it()` or `suite()`/`test()`
still allows randomization, because sibling tests are queued together.

Example: this remains eligible for randomization.

```mjs
import { describe, it } from 'node:test';

describe('math', () => {
it('adds', () => {});
it('subtracts', () => {});
it('multiplies', () => {});
});
```

```cjs
const { describe, it } = require('node:test');

describe('math', () => {
it('adds', () => {});
it('subtracts', () => {});
it('multiplies', () => {});
});
```

`--test-randomize` and `--test-random-seed` are not supported with `--watch` mode.

Matching files are executed as test files.
More information on the test file execution can be found
in the [test runner execution model][] section.
Expand Down Expand Up @@ -625,6 +713,10 @@ test runner functionality:
* `--test-reporter` - Reporting is managed by the parent process
* `--test-reporter-destination` - Output destinations are controlled by the parent
* `--experimental-config-file` - Config file paths are managed by the parent
* `--test-randomize` - Randomization is managed by the parent process and
propagated to child processes
* `--test-random-seed` - Randomization seed is managed by the parent process and
propagated to child processes

All other Node.js options from command line arguments, environment variables,
and configuration files are inherited by the child processes.
Expand Down Expand Up @@ -1531,6 +1623,13 @@ changes:
that specifies the index of the shard to run. This option is _required_.
* `total` {number} is a positive integer that specifies the total number
of shards to split the test files to. This option is _required_.
* `randomize` {boolean} Randomize execution order for test files and queued tests.
This option is not supported with `watch: true`.
**Default:** `false`.
* `randomSeed` {number} Seed used when randomizing execution order. If this
option is set, runs can replay the same randomized order deterministically,
and setting this option also enables randomization.
**Default:** `undefined`.
* `rerunFailuresFilePath` {string} A file path where the test runner will
store the state of the tests to allow rerunning only the failed tests on a next run.
see \[Rerunning failed tests]\[] for more information.
Expand Down
16 changes: 16 additions & 0 deletions doc/node-config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,14 @@
"type": "boolean",
"description": "run tests with 'only' option set"
},
"test-random-seed": {
"type": "number",
"description": "seed used to randomize test execution order"
},
"test-randomize": {
"type": "boolean",
"description": "run tests in a random order"
},
"test-reporter": {
"oneOf": [
{
Expand Down Expand Up @@ -910,6 +918,14 @@
"type": "boolean",
"description": "run tests with 'only' option set"
},
"test-random-seed": {
"type": "number",
"description": "seed used to randomize test execution order"
},
"test-randomize": {
"type": "boolean",
"description": "run tests in a random order"
},
"test-reporter": {
"oneOf": [
{
Expand Down
20 changes: 20 additions & 0 deletions doc/node.1
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,22 @@ tests must satisfy \fBboth\fR requirements in order to be executed.
Configures the test runner to only execute top level tests that have the \fBonly\fR
option set. This flag is not necessary when test isolation is disabled.
.
.It Fl -test-random-seed
Set the seed used to randomize test execution order.
This applies to both test file execution order and queued tests within each file.
Providing this flag enables randomization implicitly, even without
\fB--test-randomize\fR.
The value must be an integer between 0 and 4294967295.
This flag cannot be used with \fB--watch\fR or \fB--test-rerun-failures\fR.
.
.It Fl -test-randomize
Randomize test execution order.
This applies to both test file execution order and queued tests within each file.
This can help detect tests that rely on shared state or execution order.
The seed used for randomization is printed in the test summary and can be
reused with \fB--test-random-seed\fR.
This flag cannot be used with \fB--watch\fR or \fB--test-rerun-failures\fR.
.
.It Fl -test-reporter
A test reporter to use when running tests. See the documentation on
test reporters for more details.
Expand Down Expand Up @@ -2034,6 +2050,10 @@ one is included in the list below.
.It
\fB--test-reporter-destination\fR
.It
\fB--test-randomize\fR
.It
\fB--test-random-seed\fR
.It
\fB--test-reporter\fR
.It
\fB--test-rerun-failures\fR
Expand Down
Loading
Loading