Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Jest error: test function takes done and also returns a promise

ErrorJest throws an error for a test function that takes done and returns a promise

Short answer

Jest refuses a test function that takes a done argument and also returns a promise, as a precaution against memory leaks. An async function always returns a promise, so async (done) => {} breaks it. Remove done and await the work, or remove async and return nothing.

  • Applies to Jest 29, Jest 30, NestJS 10, NestJS 11, ts-jest
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find the test the error names and look at its signature: does it list done, and is it async or does it return something?

    npx jest --testPathPattern users.service
    grep -rnE "async \(done\)|\(done\) =>|done\)" test src | head -n 20
  2. 2Match what you see, then jump to its fix

    1. 1it('...', async (done) => { ... })An async test function also takes doneFix
    2. 2it('...', (done) => service.run().then(() => done()))An arrow function with done returns an expressionFix
    3. 3The test fails with a timeout errordone is never reached, so the test times outFix

What does the error look like?

  • Jest throws an error for a test function that takes done and returns a promise
  • An async test written as async (done) => {} fails before it runs
  • A done-style test fails with a timeout error

Cause 1 of 3

An async test function also takes done

1Check

Jest's docs say it throws when the same test function is passed a done callback and returns a promise. An async function returns a promise.

grep -rn "async (done)" src test

2Fix

Remove done and let Jest wait for the promise.

it('creates a user', async () => {
  const user = await service.create({ name: 'Ali' })
  expect(user.id).toBeDefined()
})

3Verify

Run the test file again.

npx jest users.service

Confirmed by jestjs.io

Did this fix it?

Cause 2 of 3

An arrow function with done returns an expression

1Check

A concise arrow body returns the promise from then(), so the test both takes done and returns a promise.

grep -rn "done) =>" src test

2Fix

Use braces so nothing is returned, or switch to the promise style and drop done.

it('runs', (done) => {
  service.run().then(() => {
    expect(true).toBe(true)
    done()
  })
})

3Verify

Run the test; it ends when done() is called.

npx jest users.service

Confirmed by jestjs.io

Did this fix it?

Cause 3 of 3

done is never reached, so the test times out

1Check

With the done style, Jest waits until done() is called. If an assertion throws first, done() is skipped.

grep -rn -B3 "done()" test | head -n 40

2Fix

Wrap assertions in try/catch and call done(error) on failure, or move to async and await.

try {
  expect(data).toBe('ok')
  done()
} catch (error) {
  done(error)
}

3Verify

Break the assertion on purpose; the test fails with that error and not a timeout.

npx jest users.service

Confirmed by jestjs.io

Did this fix it?

How do I stop it coming back?

  • Use async and await for new tests; keep done only for callback APIs.
  • Do not mix the two styles in one test function.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

NestJS GraphQL cannot work out the output type of a field or query

Cannot determine a GraphQL output type for the "<name>". Make sure your class is decorated with an appropriate decorator.

In code-first mode Nest reads TypeScript types through decorators, and arrays, promises and unions disappear at runtime.

2 causes, each with a fix

Back end

IntelliJ IDEA breakpoints are ignored in a NestJS app

No 'Debugger listening' line appears when the NestJS app starts

IntelliJ can only debug a NestJS app when the Node.js process runs with the inspector on and the IDE attaches to its port.

4 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.