-
-
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
Open
pmarchini
wants to merge
4
commits into
nodejs:main
Choose a base branch
from
pmarchini:test_runner/deterministic-test-randomizer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4d626b6
test_runner: support test order randomization
pmarchini 0ed6cee
fixup! test_runner: support test order randomization
pmarchini 52fd1a8
fixup! test_runner: support test order randomization
pmarchini 84b6260
fixup! test_runner: support test order randomization
pmarchini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
|
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. | ||
|
|
@@ -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. | ||
|
|
@@ -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. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.