Concept: Testing Promises
Question
Which is a valid way to test a function returning a Promise?
Hint
Jest handles returned Promises from test callbacks.
Explanation
Returning a Promise from the test callback is a valid approach — Jest will wait for the Promise to resolve (or reject) before completing the test. The other common approach is using async/await. Using setTimeout is unreliable. For rejected promises, use expect(promise).rejects.toThrow() or try/catch with async/await.
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.