Skip to content

Jest or Vitest worker ran out of memory

A Jest or Vitest worker that runs out of memory is often one of several test runs at once. Lower maxWorkers and queue whole runs across agents.

On this page

The test suite crashes, not with a failing assertion but with a memory error. Jest says a worker ran out of memory and crashed, or that a worker hit its retry limit. Vitest says a worker was terminated because it reached its memory limit. Node prints FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory. Run the same suite again later and it passes.

There are two different problems behind these lines. One is inside a single run. The other is several runs at once. The fixes are different, so it pays to tell them apart first.

What the messages mean

  • JavaScript heap out of memory comes from Node. The process reached its heap limit, which Node sets by default and you can raise with --max-old-space-size. This is a limit inside one process, not the machine running out.
  • "Worker terminated due to reaching memory limit" is the same heap limit, hit inside a worker thread. Node prints it, and Vitest shows it when it runs tests in threads (pool: 'threads'). With Vitest's default pool, forks, a worker that hits the limit prints the heap message above instead.
  • A Jest worker that crashes or exits with SIGKILL can mean the heap limit, or that the operating system killed it because the whole machine ran out of memory.

A heap limit is about one process growing too large. A system kill is about all processes together being too large.

Why it happens

Jest and Vitest both run test files in parallel workers. By default they pick the worker count from your CPU cores. Each worker holds its own copy of the modules it loads, so memory grows with the worker count.

That is manageable for one run. It stops being manageable when several runs overlap. If three coding agents each run the full suite at the same moment, you have three times the workers. The default of Jest and of Vitest outside watch mode is one worker fewer than the cores, so on an 8-core machine three runs mean 21 workers, each with its own heap. No single run is wrong. The total is.

A real memory leak in the tests also shows up this way: the heap of one worker grows file after file until it hits the limit. That one is in your code and needs fixing in your code.

How to diagnose it without any extra tool

  1. Run the suite alone on an idle machine. If it still runs out of memory, the cause is inside the run: a leak, a huge fixture, or too many workers for the machine.
  2. Watch the heap per test file. Jest has --logHeapUsage, which prints the heap after each test file. A number that only grows points to a leak.
  3. Lower the worker count. Try jest --maxWorkers=2 or vitest --maxWorkers=2. If the crash goes away, memory per run was the problem.
  4. Recycle workers. Jest's workerIdleMemoryLimit checks a worker's memory after each test file and restarts the worker when it is above the limit, which hides a slow leak. Vitest's default pool, forks, already runs tests in child processes. If a project set pool: 'threads' or 'vmThreads', try --pool=forks; the vmThreads pool is known to leak memory and restarts its workers at vmMemoryLimit.
  5. Check what else is running. ps -Ao pid,rss,command | grep -E "jest|vitest" lists every test process on the machine. Two or more separate suites means overlap.
  6. Check the system log if a worker died by SIGKILL. On Linux, dmesg -T | grep -i "killed process" shows whether the out-of-memory killer acted.

Raising --max-old-space-size helps when one process needs a bigger heap. It makes the overlap problem worse, because every worker of every run may now grow larger.

How dotime-queue helps with the overlap

dotime-queue is one waiting line for heavy commands on a computer. Keep your per-run worker cap, and start the whole run through the queue:

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

Then whole runs take turns. By default at most 2 run at once, and a run waits while free memory is under 20 percent or the disk is low. The workers of one run stay as Jest or Vitest set them; the queue limits how many runs share the machine.

To make agents do this by themselves, run this once in the project:

Shell
dotime-queue setup --write

It adds the queue instruction to AGENTS.md and CLAUDE.md. Without --write it only prints what it would add.

Other things you get from the same command:

  • A reason for each failure. When the output goes to a file, a run whose output shows JavaScript heap out of memory or ENOMEM gets the class memory. dotime-queue reasons counts the last 7 days of failures by class. A terminal or a pipe is never read, so those failures stay unknown.
  • No repeat of a suite that passed. A test run that already passed on the same clean commit, in the same folder, with the same command, is skipped and exits 0. --no-skip runs it anyway.
  • A run limit. A hung suite is stopped at its limit with exit 124, with every process it started.

The trade-off: some runs wait instead of starting at once. In the project's benchmark, where 4 agents started the same suite together, every agent had a green run after 57 to 58 seconds through the queue. Started directly, it took 373 seconds in one run, and in two runs some agents were still red after 8 tries. That suite is a stand-in, not a real project. See the benchmarks page.

What it does not do

  • It does not fix a memory leak in your tests. A run that leaks will still hit its heap limit, alone or not.
  • It does not change Jest's or Vitest's worker count. Set maxWorkers yourself.
  • It only holds runs started through the queue. An agent that runs npm test directly is not held.
  • It works on one macOS or Linux computer. It does not change CI runners or cloud test services.