Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Next.js and React

Jest gives up on a test worker that keeps exiting

Your error

Jest worker encountered 4 child process exceptions, exceeding retry limit2 more lines it can show

Fix it fast

Most common cause, 1 of 2

Code under test calls process.exit with an error code

Keep process.exit out of modules that tests import: throw an error or return an exit code, and exit only in the tiny entry script.

Fix
const exitSpy = jest.spyOn(process, 'exit').mockImplementation((code) => {
  throw new Error(`process.exit(${code})`);
});
expect(() => startServer({})).toThrow('process.exit(1)');
exitSpy.mockRestore();
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

"Jest worker encountered 4 child process exceptions, exceeding retry limit" means the worker running a test file exited with a non-zero code, Jest restarted it three times, and every try died again. In Jest 30, two known triggers are process.exit() in the code under test and a custom test environment without getVmContext. The real error is in stderr above it.

  • Applies to Jest 30.5, jest-environment-jsdom 30.5, jest-environment-node 30.5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Run only the file that fails and filter its output for the two lines Jest prints just before the process exits. A single file may run inside the main Jest process, so you might not see the retry message at all, only the line that names the cause.

Run this first
npx jest src/the-failing.test.js 2>&1 | grep -E 'process.exit called with|getVmContext|exceeding retry limit'

Then find the line you see in this list and open its fix:

  1. 1Code under test calls process.exit with an error codestderr shows process.exit called with "1" (or another number) followed by a stack trace into your own file
  2. 2A test environment written for Jest 26 or olderstderr says Test environment found at "..." does not export a "getVmContext" method, which is mandatory from Jest 27

What does the error look like?

  • Jest worker encountered 4 child process exceptions, exceeding retry limit
  • process.exit called with "1"
  • does not export a "getVmContext" method, which is mandatory from Jest 27

Cause 1 of 2

Code under test calls process.exit with an error code

You seestderr shows process.exit called with "1" (or another number) followed by a stack trace into your own file

Check

The retry-limit error appears when a Jest worker exits with a non-zero code over and over, and the most direct way to cause that is a process.exit call in the module being tested. Jest wraps process.exit inside each test file, prints the call with its stack, then lets the exit happen, so the worker dies on every retry. Jest's own end-to-end test reproduces the message with process.exit(134).

Check
grep -rn "process.exit(" src --include=*.js --include=*.ts

Fix

Keep process.exit out of modules that tests import: throw an error or return an exit code, and exit only in the tiny entry script. Where a test must cover the exit path, replace process.exit with a spy that throws.

Fix
const exitSpy = jest.spyOn(process, 'exit').mockImplementation((code) => {
  throw new Error(`process.exit(${code})`);
});
expect(() => startServer({})).toThrow('process.exit(1)');
exitSpy.mockRestore();

Verify

The file now passes, or fails with a normal assertion message; the retry-limit line is gone.

Verify
npx jest src/the-failing.test.js

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 2

A test environment written for Jest 26 or older

You seestderr says Test environment found at "..." does not export a "getVmContext" method, which is mandatory from Jest 27

Check

Jest's test runner checks that the environment class has getVmContext. When it is missing, the runner logs that message and calls process.exit(1) inside the worker, which again ends in the retry-limit error. It comes from a hand-written environment or an old third-party jest-environment package left behind after a Jest upgrade.

Check
npm ls jest jest-environment-jsdom jest-environment-node

Fix

Install the environment package at the same major version as jest, and point testEnvironment at it. A custom environment should extend the class exported by jest-environment-node or jest-environment-jsdom so it inherits getVmContext.

Fix
npm install --save-dev jest@30 jest-environment-jsdom@30
# jest.config.js
# module.exports = { testEnvironment: 'jsdom' };

Verify

npm ls shows one major version for jest and every jest-environment package, and the suite starts without the getVmContext line.

Verify
npx jest --listTests && npx jest

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Add a lint rule or a code review check that flags process.exit outside the CLI entry file, since any imported module that exits will take a Jest worker down.
  • Upgrade jest and every jest-environment-* or preset package in the same pull request, and print npm ls in CI so a second Jest major stands out.
  • Keep one test in CI that covers the exit path with a process.exit spy, so the behaviour stays tested without killing workers.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.