@@ -585,6 +585,94 @@ prevent shell expansion, which can reduce portability across systems.
585585node --test " **/*.test.js" " **/*.spec.js"
586586```
587587
588+ ### Randomizing tests execution order
589+
590+ <!-- YAML
591+ added: REPLACEME
592+ -->
593+
594+ > Stability: 1.0 - Early development
595+
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.
600+
601+ ``` bash
602+ node --test --test-randomize
603+ ```
604+
605+ When randomization is enabled, the test runner prints the seed used for the run
606+ as a diagnostic message:
607+
608+ ``` text
609+ Randomized test order seed: 12345
610+ ```
611+
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:
615+
616+ ``` bash
617+ node --test --test-randomize --test-random-seed=12345
618+ ```
619+
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+
674+ ` --test-randomize ` and ` --test-random-seed ` are not supported with ` --watch ` mode.
675+
588676Matching files are executed as test files.
589677More information on the test file execution can be found
590678in the [ test runner execution model] [ ] section.
@@ -625,6 +713,10 @@ test runner functionality:
625713* ` --test-reporter ` - Reporting is managed by the parent process
626714* ` --test-reporter-destination ` - Output destinations are controlled by the parent
627715* ` --experimental-config-file ` - Config file paths are managed by the parent
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
628720
629721All other Node.js options from command line arguments, environment variables,
630722and configuration files are inherited by the child processes.
@@ -1531,6 +1623,13 @@ changes:
15311623 that specifies the index of the shard to run. This option is _ required_ .
15321624 * ` total ` {number} is a positive integer that specifies the total number
15331625 of shards to split the test files to. This option is _ required_ .
1626+ * ` randomize ` {boolean} Randomize execution order for test files and queued tests.
1627+ This option is not supported with ` watch: true ` .
1628+ ** Default:** ` false ` .
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.
1632+ ** Default:** ` undefined ` .
15341633 * ` rerunFailuresFilePath ` {string} A file path where the test runner will
15351634 store the state of the tests to allow rerunning only the failed tests on a next run.
15361635 see \[ Rerunning failed tests] \[ ] for more information.
0 commit comments