Mistake: Missing done Callback
Question
What is wrong with this callback test?
Code
test('callback test', () => {
fetchData((data) => {
expect(data).toBe('result');
});
});
Hint
Jest finishes the test before the callback runs.
Explanation
The mistake is not using the done callback. Jest sees the test function returns synchronously and marks it as complete before fetchData's callback runs. The assertion may never execute. The fix: test('callback test', (done) => { fetchData((data) => { expect(data).toBe('result'); done(); }); }); — Jest waits for done() to be called.
Related Resources
Related Lessons
- Async Testing
Testing asynchronous code.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.