Skip to content
Latchkey

Jest "Cannot log after tests are done" in CI

A test completed, but code it kicked off kept running and called console.log afterward. Jest flags the late log because the async work was never awaited - a sign of a leak that can also cause flaky failures.

What this error means

A warning appears: "Cannot log after tests are done. Did you forget to wait for something async in your test?" The log line that triggered it points at the unawaited operation.

jest
Cannot log after tests are done. Did you forget to wait for something
async in your test?
Attempted to log "fetched user 42".

      at console.log (src/user.ts:18:11)

Common causes

Unawaited async work continues after the test

A fire-and-forget promise (a floating fetch, a background task) resolves and logs after the test that started it already finished.

A timer or listener still firing

A setTimeout/setInterval or event handler created during the test runs after teardown and logs into a finished test's scope.

How to fix it

Await every async operation

Return or await the work so the test does not end before it completes.

user.test.ts
it('loads user', async () => {
  await expect(loadUser(42)).resolves.toBeDefined();
});

Stop background work in teardown

  1. Clear timers/intervals created in the test in afterEach.
  2. Remove event listeners and close any started servers.
  3. Run --detectOpenHandles to locate what is still running.

How to prevent it

  • Never fire-and-forget promises in tests; await them.
  • Tear down timers, listeners, and servers in afterEach.
  • Treat the warning as a failure in CI to catch leaks early.

Frequently asked questions

What causes ""Cannot log after tests are done""?
A fire-and-forget promise (a floating fetch, a background task) resolves and logs after the test that started it already finished.
How do I fix "Cannot log after tests are done"?
Return or await the work so the test does not end before it completes.

Related guides

References

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