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.
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.
it('loads user', async () => {
await expect(loadUser(42)).resolves.toBeDefined();
});Stop background work in teardown
- Clear timers/intervals created in the test in
afterEach. - Remove event listeners and close any started servers.
- Run
--detectOpenHandlesto 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.