Skip to content

Missing done() Callback in Async Test

Error message

                Test finished before async callback completed — done() not called
              

Quick summary

The test uses the done callback pattern but forgets to call done() inside the async callback. Jest marks the test as complete before the assertion runs.

Why this happens

  • The done parameter is declared but done() is never called.

  • done() is called in the wrong place (e.g., before the assertion).

  • An error in the callback prevents done() from being reached.

  • Mixing async/await with done callback incorrectly.

Minimal example

✗ Broken code
test('callback test', (done) => {
  fetchData((data) => {
    expect(data).toBe('result');
    // missing done() — Jest finishes early
  });
});
✓ Fixed code
test('callback test', (done) => {
  fetchData((data) => {
    expect(data).toBe('result');
    done(); // signal test completion
  });
});

Call done() after all assertions to tell Jest the async test is complete.

How to diagnose

  • Check if the test function takes a 'done' parameter.

  • Verify done() is called at the end of the async callback, after all assertions.

  • Check if errors in the callback prevent done() from being called.

  • Consider switching to async/await which doesn't need done().

How to fix

  • Call done() after all assertions in the callback: callback((data) => { expect(data).toBe(...); done(); }).

  • Handle errors: callback((err, data) => { if (err) { done(err); return; } expect(data).toBe(...); done(); }).

  • Prefer async/await over done callback — it's cleaner and less error-prone.

How to prevent

  • Prefer async/await for async tests — avoid the done callback pattern.

  • If using done(), always call it in every code path (including error paths).

Related Resources

Related Lessons

← Back to language