Jest "JavaScript heap out of memory" in CI (--maxWorkers)
A Jest worker exhausted the V8 heap and crashed. Jest spawns one worker per CPU, so on a many-core CI machine the combined memory blows past the limit; a per-test leak makes it worse.
What this error means
The run dies with "FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory." There is often no assertion failure - a worker was OOM-killed, not a test failing.
<--- Last few GCs --->
[1234:0x5e0] Mark-Compact ... allocation failure
FATAL ERROR: Reached heap limit Allocation failed -
JavaScript heap out of memoryCommon causes
Too many workers for the RAM
Jest defaults to one worker per core. A 16-vCPU runner spawns ~15 workers; if each loads a heavy module graph, total memory exceeds the limit.
A memory leak across tests
Tests that retain large objects, never tear down servers, or accumulate listeners grow a worker's heap until V8 gives up.
How to fix it
Cap the worker count in CI
Fewer workers means lower peak memory - the most reliable fix on big runners.
# an absolute count or a fraction of cores
jest --maxWorkers=2
# or
jest --maxWorkers=50%Raise the Node heap and find leaks
NODE_OPTIONS=--max-old-space-size=4096 jest --maxWorkers=2
# investigate retained memory
jest --detectLeaks --logHeapUsageHow to prevent it
- Pin
--maxWorkersin CI rather than inheriting the core count. - Tear down servers, timers, and listeners in
afterEach/afterAll. - Size the runner for the suite's real memory footprint.