Asynchronous code needs Jest to know when it has completed before moving on to another test. The simplest form returns a promise from the test — Jest waits for it to resolve, and a rejection fails the test. The equivalent logic uses an async function passed to test with await before each call, optionally combined with .resolves or .rejects: await expect(fetchData()).rejects.toMatch('error'). Always return or await the promise; omitting either lets the test complete before the promise settles. When asserting rejections with .catch, add expect.assertions to verify the assertion count, otherwise a fulfilled promise would not fail the test. For callback-style APIs, declare a single done parameter and Jest waits until done is called; if done() never runs, the test fails with a timeout error, so wrap expect in try/catch and pass caught errors to done(error) to avoid opaque timeouts. Jest throws an error if one test both takes done and returns a promise, as a precaution against memory leaks. None of these forms is superior; mix and match them across a codebase or within a single file. Scope: Jest. Reviewed October 4, 2026.
Testing asynchronous code
Document v1 · published by Koskamo editorial · updated 2026-10-04
Source and provenance
- Original source
- jestjs.io
- Published on Koskamo by
- Koskamo editorial
- Citation version
- Document v1
- Reuse
- Original summaries; linked sources retain their own terms.