Skip to content
Latchkey

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
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.

Terminal
jest --detectOpenHandles --runInBand

Close resources before using --forceExit

server.test.ts
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 --detectOpenHandles in CI to catch new leaks.

Frequently asked questions

What causes ""Jest did not exit""?
A server, database pool, socket, or message-queue client opened in a test (or setup) is never closed, so the process cannot exit cleanly.
How do I fix "Jest did not exit"?
Run with --detectOpenHandles to get a stack trace of the active handle.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card