-
-
Notifications
You must be signed in to change notification settings - Fork 34.9k
test_runner: support test order randomization #61747
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
base: main
Are you sure you want to change the base?
Changes from all commits
0fb39fa
300a0a9
5745058
d013ca5
cd9a8ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -631,6 +631,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 | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure this is valid? I thought everything after the |
||||||
| ``` | ||||||
|
|
||||||
| 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 | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this change is in reference to the previous line of text that states the |
||||||
| ``` | ||||||
|
|
||||||
| 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. | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I thiiink it should be "enqueued"? "queued" together means they wait together; enqueued together means they're added to a queue one-by-one setting up a sequence. |
||||||
|
|
||||||
| 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. | ||||||
|
|
@@ -671,6 +759,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. | ||||||
|
|
@@ -1579,6 +1671,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 | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets mention the 0 to 2^32 - 1 limit here. |
||||||
| 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. | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.