Testing Promises & Async/Await in Jest
From the Jest cheat sheet · Async Testing · verified Jul 2026
Testing Promises & Async/Await
Different patterns for testing asynchronous code
javascript
// Using async/await
test('async data fetch', async () => {
const data = await fetchData()
expect(data).toBe('data')
})
// Using promises
test('promise resolution', () => {
return fetchData().then(data => {
expect(data).toBe('data')
})
})
// Expect promises
test('resolves/rejects', async () => {
await expect(fetchData()).resolves.toBe('data')
await expect(fetchError()).rejects.toThrow('error')
})
// Testing callbacks
test('callback test', (done) => {
function callback(data) {
try {
expect(data).toBe('data')
done()
} catch (error) {
done(error)
}
}
fetchDataCallback(callback)
})💡 Always return or await promises in tests to avoid false positives
⚡ Use expect.assertions(n) to ensure async assertions run
📌 resolves/rejects matchers make async tests cleaner
🟢 Set custom timeout as third parameter for slow operations
asyncpromises