Skip to content

Tests pass alone but fail when run in parallel

Tests that pass alone but time out in parallel often mean an overloaded machine, not bad code. How to tell, and how to cap parallel runs safely.

On this page

Each test passes when you run it by itself. Run the suite in parallel, or run two suites at once, and some of them fail. The failures move around: a different test is red each time. It is tempting to start changing code. Before you do, find out which of two very different causes you have.

Two families of causes

Shared state. The tests depend on something they share, and running them together breaks it. This is a real bug in the tests or the code:

  • two tests use the same database rows, the same table, or the same temporary folder;
  • two test servers try to listen on the same port (EADDRINUSE);
  • one test changes a global value, an environment variable or a mocked clock that another test reads;
  • tests depend on running in a fixed order.

An overloaded machine. Nothing is shared, but the computer cannot keep up:

  • tests with timeouts fail because CPU time is split between too many processes;
  • a database or a browser under test answers slowly and requests time out;
  • memory runs out, swap starts, and workers are killed (exit 137) or crash with a heap error.

This second family is common when several coding agents work on one computer. Each agent starts its own full test run. Three suites at once can ask for far more CPU and memory than the machine has. Each agent sees only its own red run, and may then "fix" code that was never broken.

A quick test to tell them apart

  1. Run the suite alone, in one worker, on an idle machine. Jest: --runInBand. Vitest: --no-file-parallelism. If tests still fail, the cause is in the tests, not in parallelism.
  2. Run it in parallel on an idle machine. Close other suites and builds first. If it now fails, the tests share state with each other. Look for the same port, database or temp folder used by two test files.
  3. Run it alone, but with the machine under load. Start a second suite or a build at the same time. If tests that passed in step 2 now time out, the cause is load.
  4. Shuffle the order. Jest has --randomize, which shuffles the tests within each file, and Vitest has --sequence.shuffle, which shuffles files and tests. Jest prints the seed it used and --seed repeats that order; in Vitest, set --sequence.seed. A test that fails only in some orders depends on another test.
  5. Look at the machine while it fails. Compare uptime with your core count (nproc on Linux, sysctl -n hw.ncpu on macOS). A load far above the cores means work is waiting for CPU. Check free memory and swap, and lsof -i :<port> for port clashes.

The error text helps too. EADDRINUSE is a port clash. Timeout and ETIMEDOUT lean towards load. A changing set of failing tests leans towards load or order. The same test failing every time leans towards shared state.

Fixing shared state

This part is your code, and no queue fixes it:

  • give each test worker its own database or schema, named from the worker id (JEST_WORKER_ID in Jest, VITEST_POOL_ID in Vitest);
  • let the system pick a free port (listen on port 0) instead of a fixed one;
  • create temp folders with a unique name per test;
  • reset globals, mocks and timers after each test.

Fixing load

Within one run, cap the workers: --maxWorkers=2 in Jest or Vitest. That leaves room for other work.

Across runs, the cap has to cover the whole machine, because the runs do not see each other. That is what dotime-queue does. Start each full run through it:

Shell
dotime-queue run --label "web-app: tests" -- npm test > test.log 2>&1dotime-queue status

By default at most 2 jobs run at once, more on a computer with more than 5 cores while the CPU has room. A job also waits while memory, disk or CPU is short. Run dotime-queue setup --write once in the project so agents start their test runs this way by themselves.

It also helps you read the results:

  • Flaky flag. When a check both passed and failed on the same clean commit, a failed run prints a flaky: line: the failure may not come from the change at hand. dotime-queue flaky lists those checks.
  • Failure reasons. When the output goes to a file, each failed run gets a class such as port, memory, test or network. dotime-queue reasons counts them for the last 7 days, and how many passed on a plain re-run.

The project's benchmark shows the load family on its own. Four agents started the same suite at once on one Mac; the code never changed. Started directly, 400 of 400 tests failed on the first try in every run. Through the queue, 0 of 400 did. It is a stand-in suite, so your numbers will differ. See the benchmarks page.

What it does not do

  • It does not fix tests that share state or depend on order. Those still fail, now without the noise of an overloaded machine, which makes them easier to see.
  • It does not split one suite across workers. Your test runner still does that.
  • It only holds runs started through the queue, on one macOS or Linux computer.
  • The flaky flag is advice only. It changes no exit code and runs nothing again.