Jest "did not exit one second after the test run completed"
Tests passed, but Jest could not exit within a second because asynchronous operations are still active - an open server, a DB connection, or a timer. In CI this can hang the job until a step timeout kills it.
What this error means
After results print, Jest warns "Jest did not exit one second after the test run completed. This usually means that there are asynchronous operations that weren't stopped." The job hangs even though all tests passed.
Jest did not exit one second after the test run completed.
This usually means that there are asynchronous operations that weren't
stopped in your tests. Consider running Jest with `--detectOpenHandles`
to troubleshoot this issue.Common causes
Open async resources
A server, database pool, socket, or message-queue client opened in a test (or setup) is never closed, so the process cannot exit cleanly.
Scheduled timers still pending
A setInterval or library polling loop is still scheduled when tests finish, keeping the event loop alive past the one-second grace.
How to fix it
Find what is still open
Run with --detectOpenHandles to get a stack trace of the active handle.
jest --detectOpenHandles --runInBandClose resources before using --forceExit
afterAll(async () => {
await new Promise((r) => server.close(r));
await db.end();
clearInterval(pollTimer);
});How to prevent it
- Close every server, pool, and socket in
afterAll. - Clear timers/intervals; prefer fake timers where possible.
- Run
--detectOpenHandlesin CI to catch new leaks.