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 showFix it fast
Most common cause, 1 of 2Code 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.
const exitSpy = jest.spyOn(process, 'exit').mockImplementation((code) => {
throw new Error(`process.exit(${code})`);
});
expect(() => startServer({})).toThrow('process.exit(1)');
exitSpy.mockRestore();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.
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.
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:
- 1Code under test calls process.exit with an error code
stderr shows process.exit called with "1" (or another number) followed by a stack trace into your own file - 2A test environment written for Jest 26 or older
stderr 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 limitprocess.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).
grep -rn "process.exit(" src --include=*.js --include=*.tsFix
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.
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.
npx jest src/the-failing.test.jsConfirmed by raw.githubusercontent.com
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.
npm ls jest jest-environment-jsdom jest-environment-nodeFix
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.
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.
npx jest --listTests && npx jestConfirmed by raw.githubusercontent.com
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.