@@ -631,6 +631,94 @@ prevent shell expansion, which can reduce portability across systems.
631631node --test " **/*.test.js" " **/*.spec.js"
632632```
633633
634+ ### Randomizing tests execution order
635+
636+ <!-- YAML
637+ added: REPLACEME
638+ -->
639+
640+ > Stability: 1.0 - Early development
641+
642+ The test runner can randomize execution order to help detect
643+ order-dependent tests. When enabled, the runner randomizes both discovered
644+ test files and queued tests within each file. Use ` --test-randomize ` to
645+ enable this mode.
646+
647+ ``` bash
648+ node --test --test-randomize
649+ ```
650+
651+ When randomization is enabled, the test runner prints the seed used for the run
652+ as a diagnostic message:
653+
654+ ``` text
655+ Randomized test order seed: 12345
656+ ```
657+
658+ Use ` --test-random-seed=<number> ` to replay the same randomized order
659+ deterministically. Supplying ` --test-random-seed ` also enables randomization,
660+ so ` --test-randomize ` is optional when a seed is provided:
661+
662+ ``` bash
663+ node --test --test-randomize --test-random-seed=12345
664+ ```
665+
666+ In most test files, randomization works automatically. One important exception
667+ is when subtests are awaited one by one. In that pattern, each subtest starts
668+ only after the previous one finishes, so the runner keeps declaration order
669+ instead of randomizing it.
670+
671+ Example: this runs sequentially and is ** not** randomized.
672+
673+ ``` mjs
674+ import test from ' node:test' ;
675+
676+ test (' math' , async (t ) => {
677+ for (const name of [' adds' , ' subtracts' , ' multiplies' ]) {
678+ // Sequentially awaiting each subtest preserves declaration order.
679+ await t .test (name, async () => {});
680+ }
681+ });
682+ ```
683+
684+ ``` cjs
685+ const test = require (' node:test' );
686+
687+ test (' math' , async (t ) => {
688+ for (const name of [' adds' , ' subtracts' , ' multiplies' ]) {
689+ // Sequentially awaiting each subtest preserves declaration order.
690+ await t .test (name, async () => {});
691+ }
692+ });
693+ ```
694+
695+ Using suite-style APIs such as ` describe() ` /` it() ` or ` suite() ` /` test() `
696+ still allows randomization, because sibling tests are queued together.
697+
698+ Example: this remains eligible for randomization.
699+
700+ ``` mjs
701+ import { describe , it } from ' node:test' ;
702+
703+ describe (' math' , () => {
704+ it (' adds' , () => {});
705+ it (' subtracts' , () => {});
706+ it (' multiplies' , () => {});
707+ });
708+ ```
709+
710+ ``` cjs
711+ const { describe , it } = require (' node:test' );
712+
713+ describe (' math' , () => {
714+ it (' adds' , () => {});
715+ it (' subtracts' , () => {});
716+ it (' multiplies' , () => {});
717+ });
718+ ```
719+
720+ ` --test-randomize ` and ` --test-random-seed ` are not supported with ` --watch ` mode.
721+
634722Matching files are executed as test files.
635723More information on the test file execution can be found
636724in the [ test runner execution model] [ ] section.
@@ -671,6 +759,10 @@ test runner functionality:
671759* ` --test-reporter ` - Reporting is managed by the parent process
672760* ` --test-reporter-destination ` - Output destinations are controlled by the parent
673761* ` --experimental-config-file ` - Config file paths are managed by the parent
762+ * ` --test-randomize ` - Randomization is managed by the parent process and
763+ propagated to child processes
764+ * ` --test-random-seed ` - Randomization seed is managed by the parent process and
765+ propagated to child processes
674766
675767All other Node.js options from command line arguments, environment variables,
676768and configuration files are inherited by the child processes.
@@ -1579,6 +1671,13 @@ changes:
15791671 that specifies the index of the shard to run. This option is _ required_ .
15801672 * ` total ` {number} is a positive integer that specifies the total number
15811673 of shards to split the test files to. This option is _ required_ .
1674+ * ` randomize ` {boolean} Randomize execution order for test files and queued tests.
1675+ This option is not supported with ` watch: true ` .
1676+ ** Default:** ` false ` .
1677+ * ` randomSeed ` {number} Seed used when randomizing execution order. If this
1678+ option is set, runs can replay the same randomized order deterministically,
1679+ and setting this option also enables randomization.
1680+ ** Default:** ` undefined ` .
15821681 * ` rerunFailuresFilePath ` {string} A file path where the test runner will
15831682 store the state of the tests to allow rerunning only the failed tests on a next run.
15841683 see \[ Rerunning failed tests] \[ ] for more information.
0 commit comments