To investigate order-dependent failures with node:test, randomize the suite, record the seed, replay the failing run, and reduce it to a small reproducer. Keep ordering, concurrency, and isolation as separate experimental variables.
Node.js documents test-order randomization as added in v26.1.0 and marks it Stability 1.0 — Early development. It randomizes both discovered test files and queued tests within each file. Check your runtime version before using these options. Node.js randomization documentation
Enable randomization and replay a failure
Start with:
node --version
node --test --test-randomize
With randomization enabled, the runner prints a diagnostic containing the seed, in this form:
Randomized test order seed: 12345
To replay that randomized order, substitute the seed from your run:
node --test --test-random-seed=12345
Supplying a seed also enables randomization, so adding --test-randomize to the replay command is unnecessary. Neither flag is supported with --watch. Randomization options and replay behavior
Keep the original file arguments and other runner options when replaying. Treat the seed as an ordering input, not a complete snapshot of the experiment: preserve the Node.js version, code revision, command, selected tests, runner configuration, and relevant fixture state alongside the failure output. Avoid recording credentials or sensitive fixture contents.
Make sure your tests are eligible for shuffling
How you register tests matters. If a parent creates a subtest, awaits it, and only then creates the next subtest, the runner preserves that sequence rather than randomizing it. Suite-style APIs such as describe() and it() allow sibling tests to be queued together and remain eligible for randomization. Test registration and randomization
Here is a deliberately broken example. Save it as order-dependency.test.mjs, separate from your real suite:
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
const config = { backend: 'memory' };
describe('backend configuration', () => {
it('selects the disk backend', () => {
config.backend = 'disk';
assert.equal(config.backend, 'disk');
});
it('starts with the memory backend', () => {
assert.equal(config.backend, 'memory');
});
});
Run it with:
node --test --test-randomize order-dependency.test.mjs
In a sequential execution, the default-value assertion passes if it runs before the mutation and fails if it runs afterward. The problem is the shared config object, not the randomizer. Shuffling exposes the dependency.
When replaying this example, retain the filename and substitute the printed seed:
node --test --test-random-seed=12345 order-dependency.test.mjs
For this example, the better fixture design is to create a fresh configuration for each test. If the application deliberately uses a singleton, investigate how to establish and restore its state instead of relying on another test to leave it usable.
Search several orders and keep the logs
A single randomized run explores only one ordering. This POSIX-shell loop tries five seeds against the example file:
for seed in 1 2 3 4 5; do
if node --test --test-random-seed="$seed" order-dependency.test.mjs >"order-$seed.log" 2>&1; then
printf 'PASS seed=%s\n' "$seed"
else
printf 'FAIL seed=%s; inspect order-%s.log\n' "$seed" "$seed"
fi
done
For your own suite, replace the example filename with your normal test selection and retain your other runner options. Keep the logs and rerun a failing seed before changing the suite. Passing all five runs is not proof that the tests are independent; it is simply the result of that limited search.
For an ambiguous failure, add temporary diagnostics at the start and end of suspected test bodies. Record useful state, such as a fixture identifier or configuration value, rather than assuming the final failing assertion identifies the test that introduced the problem.
Change ordering, concurrency, and isolation separately
Start by varying only the seed under your existing configuration. Then make targeted comparisons.
Concurrency: with process isolation enabled, compare the failing run with one test-file process at a time. For a suite using default test discovery, the command is:
node --test --test-random-seed=12345 --test-concurrency=1
If the original run used explicit file arguments or other options, keep those too.
With process-level isolation enabled, --test-concurrency controls the maximum number of child processes running test files. It is not a blanket guarantee that every operation inside a file is sequential; review any test-level concurrency settings separately. Test runner execution model
If the failure disappears, treat that as a clue to investigate overlap or contention—not as proof that ordering is irrelevant.
Isolation: the documented default is process isolation, with each test file executed in a separate child process. With isolation disabled, files are imported into the runner process. After all files have loaded, their top-level tests execute with concurrency one. Tests from different files can then interact through shared global state. Isolation behavior
Keep isolation unchanged during the initial seed search. If you later compare isolation modes, make that a separate experiment. Also keep shared paths, database fixtures, and ports on the investigation checklist; do not rule out external-resource interactions just because files run in separate processes.
Reduce the failure to a revealing sequence
Once a failure replays, use this reduction process:
- Run the affected test alone. A pass suggests an interaction, but does not establish an order dependency by itself.
- Restore the failing group. Confirm that the interaction is still present under the recorded configuration.
- Remove unrelated tests in chunks. Retain a removal only when the failure still reproduces.
- Preserve the relevant relative order. After changing the selected tests, verify the order rather than assuming the same seed preserves the sequence you are investigating.
- Compare both directions. For a suspected pair, compare A → B with B → A, and run B alone.
For a small, explicitly ordered reproducer, sequentially awaited t.test() calls can be useful precisely because that registration pattern is not randomized. Sequential subtest behavior
Aim for a small, understandable reproducer rather than forcing every failure into a two-test explanation. Keep any additional setup or predecessor tests needed to reproduce it.
Inspect cleanup and work that outlives a test
Use the reduced sequence to investigate specific contamination paths:
- Module-level objects, caches, or other mutable state.
- Environment or configuration changes that are not restored.
- Mocks and fake clocks with a broader lifetime than intended.
- Reused files, database records, or other fixtures.
- Timers, requests, or background work that continue after a test ends.
For mocks, prefer the test-context API where appropriate: Node documents that mocked functionality created through t.mock is automatically restored when the test finishes. Do not treat mock restoration as a substitute for cleaning up arbitrary application state. Mocking lifecycle
Likewise, the test context’s mock timers are reset after each test completes. If you use the top-level mock tracker instead, audit where its timer state is reset. MockTimers reset behavior
Finally, inspect asynchronous completion. Node does not delay reporting a completed test to accommodate asynchronous activity that outlives it. A subtest created after its parent has finished and reported results is marked as failed. The documentation also describes failures and top-level diagnostic warnings for uncaughtException and unhandledRejection events originating from completed tests. Review those warnings as well as assertion failures, and ensure relevant asynchronous work is awaited or explicitly stopped. Extraneous asynchronous activity
After fixing the cause, rerun the small reproducer, the original failing seed, and a broader seed sweep. The goal is not to find an order that passes—it is to remove the unintended dependency.
Sources are linked throughout this guide. Product capabilities can change; consult the linked documentation for your deployment.
Read our editorial approach ↗