Jest error: test function takes done and also returns a promise
Jest throws an error for a test function that takes done and returns a promiseShort 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.
Fix it fast
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 202Match what you see, then jump to its fix
What does the error look like?
Jest throws an error for a test function that takes done and returns a promiseAn async test written as async (done) => {} fails before it runsA 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 test2Fix
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.serviceConfirmed by jestjs.io
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 test2Fix
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.serviceConfirmed by jestjs.io
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 402Fix
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.serviceConfirmed by jestjs.io
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
TypeScript cannot find module '@nestjs/core' or its type declarations
error TS2307: Cannot find module '@nestjs/common' or its corresponding type declarations.
TS2307 means TypeScript searched node_modules and found neither the package nor its types.
3 causes, each with a fixBack end
Node.js in Docker Compose cannot reach MySQL on 127.0.0.1:3306
Error: connect ECONNREFUSED 127.0.0.1:3306
Inside a container, 127.0.0.1 is that container, not MySQL.
3 causes, each with a fixBack 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 fixBack 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 fixBack end
The nest command is not found in the terminal
bash: nest: command not found
The shell cannot find a nest executable on its PATH.
3 causes, each with a fixBack end
NestJS watch mode does not restart after I save a file
Changes show on the host but not in the running container
Watch mode only sees files that exist inside the process that runs it.
3 causes, each with a fix