Jest finishes the tests but a worker keeps running and is force exited
A worker process has failed to exit gracefully and has been force exited.Short answer
Something your tests started is still alive when the tests end: a listening server, a database connection or a timer. Jest cannot stop the worker until that handle closes, so it kills it. Find the handle with --detectOpenHandles, then close it in afterAll. Do not hide it with --forceExit.
Fix it fast
1Find out which cause you have
Run Jest once with open-handle detection. It prints where each leaked resource was created. It runs tests serially, so it is slower; use it only to debug.
npx jest --detectOpenHandles2Match what you see, then jump to its fix
- 1
--detectOpenHandles points at the line with app.listenThe test starts an HTTP server and never closes itFix - 2
--detectOpenHandles lists a Timeout or an Immediate created by your codeA timer or interval is still pendingFix - 3
--detectOpenHandles points at a connection created in a beforeAll or at import timeA database or other external connection stays openFix
- 1
What does the error look like?
A worker process has failed to exit gracefully and has been force exited.
Cause 1 of 3
The test starts an HTTP server and never closes it
1Check
Search for app.listen in files the tests import. A server that listens at import time stays open for the whole run.
grep -rn "\.listen(" src2Fix
Export the app without listening, listen only in a separate start file, and when a test does start a server, close it in afterAll and wait for the callback.
let server;
beforeAll((done) => { server = app.listen(0, done); });
afterAll((done) => { server.close(done); });3Verify
Run the suite again; the warning should be gone.
npx jestConfirmed by nodejs.org
Cause 2 of 3
A timer or interval is still pending
1Check
setInterval for polling, cache cleanup or rate-limit stores keeps the event loop alive for as long as it is active.
grep -rn "setInterval\|setTimeout" src2Fix
Store the timer and clear it in a teardown hook, or call unref() on timers that must not keep the process alive.
const timer = setInterval(cleanup, 60000);
timer.unref();3Verify
Run Jest with open-handle detection again and confirm the list is empty.
npx jest --detectOpenHandlesConfirmed by nodejs.org
Cause 3 of 3
A database or other external connection stays open
1Check
Look for pools and clients opened by tests or by the modules they import, with no matching close.
grep -rn "connect(\|createPool\|new Pool" src2Fix
Close the connection in afterAll and return the promise, so Jest waits for it. The CLI docs say to tear down external resources after each test, and treat --forceExit as an escape hatch.
afterAll(async () => {
await db.end();
});3Verify
Run the full suite and watch for the message.
npx jestConfirmed by jestjs.io
How do I stop it coming back?
- Keep app creation and app.listen in separate files so tests import the app only.
- Run --detectOpenHandles in CI when the warning first appears, not months later.
- Use --runInBand only to compare behaviour, not as the permanent fix.
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.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Jest tests use more and more memory until the run slows down or dies
Memory use grows with every test file until the run slows down or is killed
Jest runs test files in worker processes, and a worker can keep memory from earlier files.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
Axios call from a React app comes back 404
404 (Not Found)
Status code 404 means a server answered, but it has nothing at that path.
3 causes, each with a fixBack end
bcrypt.hash throws 'data and salt arguments required'
Error: data and hash arguments required
The bcrypt package throws this when hash gets null or undefined for the password or for the salt.
4 causes, each with a fixBack end
bcryptjs rejects hash() or compare() with Illegal arguments
Error: Illegal arguments: string, undefined
bcryptjs checks the types of its two arguments and, when either is wrong, throws Illegal arguments followed by the typeof of the first and the second.
3 causes, each with a fixBack end
Express warns 'body-parser deprecated undefined extended'
body-parser deprecated undefined extended
The warning appears because urlencoded() was called without the extended option, which body-parser 1.x has warned about since version 1.4.0.
3 causes, each with a fix