@@ -593,8 +593,10 @@ added: REPLACEME
593593
594594> Stability: 1.0 - Early development
595595
596- The test runner can randomize the order of discovered test files to help detect
597- order-dependent tests. Use ` --test-randomize ` to enable this mode.
596+ The test runner can randomize execution order to help detect
597+ order-dependent tests. When enabled, the runner randomizes both discovered
598+ test files and queued tests within each file. Use ` --test-randomize ` to
599+ enable this mode.
598600
599601``` bash
600602node --test --test-randomize
@@ -607,14 +609,68 @@ as a diagnostic message:
607609Randomized test order seed: 12345
608610```
609611
610- Use ` --test-random-seed=<number> ` to replay the same order in a deterministic
611- way . Supplying ` --test-random-seed ` also enables randomization, so
612- ` --test-randomize ` is optional when a seed is provided:
612+ Use ` --test-random-seed=<number> ` to replay the same randomized order
613+ deterministically . Supplying ` --test-random-seed ` also enables randomization,
614+ so ` --test-randomize ` is optional when a seed is provided:
613615
614616``` bash
615617node --test --test-randomize --test-random-seed=12345
616618```
617619
620+ In most test files, randomization works automatically. One important exception
621+ is when subtests are awaited one by one. In that pattern, each subtest starts
622+ only after the previous one finishes, so the runner keeps declaration order
623+ instead of randomizing it.
624+
625+ Example: this runs sequentially and is ** not** randomized.
626+
627+ ``` mjs
628+ import test from ' node:test' ;
629+
630+ test (' math' , async (t ) => {
631+ for (const name of [' adds' , ' subtracts' , ' multiplies' ]) {
632+ // Sequentially awaiting each subtest preserves declaration order.
633+ await t .test (name, async () => {});
634+ }
635+ });
636+ ```
637+
638+ ``` cjs
639+ const test = require (' node:test' );
640+
641+ test (' math' , async (t ) => {
642+ for (const name of [' adds' , ' subtracts' , ' multiplies' ]) {
643+ // Sequentially awaiting each subtest preserves declaration order.
644+ await t .test (name, async () => {});
645+ }
646+ });
647+ ```
648+
649+ Using suite-style APIs such as ` describe() ` /` it() ` or ` suite() ` /` test() `
650+ still allows randomization, because sibling tests are queued together.
651+
652+ Example: this remains eligible for randomization.
653+
654+ ``` mjs
655+ import { describe , it } from ' node:test' ;
656+
657+ describe (' math' , () => {
658+ it (' adds' , () => {});
659+ it (' subtracts' , () => {});
660+ it (' multiplies' , () => {});
661+ });
662+ ```
663+
664+ ``` cjs
665+ const { describe , it } = require (' node:test' );
666+
667+ describe (' math' , () => {
668+ it (' adds' , () => {});
669+ it (' subtracts' , () => {});
670+ it (' multiplies' , () => {});
671+ });
672+ ```
673+
618674` --test-randomize ` and ` --test-random-seed ` are not supported with ` --watch ` mode.
619675
620676Matching files are executed as test files.
@@ -657,8 +713,10 @@ test runner functionality:
657713* ` --test-reporter ` - Reporting is managed by the parent process
658714* ` --test-reporter-destination ` - Output destinations are controlled by the parent
659715* ` --experimental-config-file ` - Config file paths are managed by the parent
660- * ` --test-randomize ` - File randomization is managed by the parent process
661- * ` --test-random-seed ` - File randomization seed is managed by the parent process
716+ * ` --test-randomize ` - Randomization is managed by the parent process and
717+ propagated to child processes
718+ * ` --test-random-seed ` - Randomization seed is managed by the parent process and
719+ propagated to child processes
662720
663721All other Node.js options from command line arguments, environment variables,
664722and configuration files are inherited by the child processes.
@@ -1565,12 +1623,12 @@ changes:
15651623 that specifies the index of the shard to run. This option is _ required_ .
15661624 * ` total ` {number} is a positive integer that specifies the total number
15671625 of shards to split the test files to. This option is _ required_ .
1568- * ` randomize ` {boolean} Randomize the execution order of test files.
1626+ * ` randomize ` {boolean} Randomize execution order for test files and queued tests .
15691627 This option is not supported with ` watch: true ` .
15701628 ** Default:** ` false ` .
1571- * ` randomSeed ` {number} Seed used when randomizing test file order. If this
1572- option is set, runs can replay the same randomized file order
1573- deterministically, and setting this option also enables randomization.
1629+ * ` randomSeed ` {number} Seed used when randomizing execution order. If this
1630+ option is set, runs can replay the same randomized order deterministically,
1631+ and setting this option also enables randomization.
15741632 ** Default:** ` undefined ` .
15751633 * ` rerunFailuresFilePath ` {string} A file path where the test runner will
15761634 store the state of the tests to allow rerunning only the failed tests on a next run.
0 commit comments