Skip to content
Latchkey

Jest "did not exit one second after the test run completed"

Your tests passed, but Jest could not exit because something is still holding the event loop open - an HTTP server, a database connection, or a timer that was never closed.

What this error means

After the results print, Jest warns "Jest did not exit one second after the test run completed" (or "A worker process has failed to exit gracefully"). In CI this can hang the job until a step timeout kills it, even though every test passed.

Jest output
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 servers or connections

An HTTP server, database pool, or socket opened in a test (or setup) is never closed, so its handle keeps the process alive.

Dangling timers or intervals

A setInterval/setTimeout or a library polling loop is still scheduled when tests finish, preventing a clean exit.

How to fix it

Find the open handle

Run with --detectOpenHandles to get a stack trace pointing at what is still open.

Terminal
jest --detectOpenHandles --runInBand

Close resources in teardown

server.test.ts
let server;
beforeAll(() => { server = app.listen(0); });
afterAll(async () => {
  await new Promise((r) => server.close(r));
  await db.end();
});

How to prevent it

  • Close every server, pool, and socket in afterAll.
  • Clear timers/intervals and use fake timers where possible.
  • Run --detectOpenHandles in CI periodically to catch new leaks.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →