Testing · Junior · Question 24 of 78
How do you test an async function that returns a promise?
test("loads user", async () => {
const user = await fetchUser(1);
expect(user.name).toBe("Ann");
});- ✓Correct answer: Make the test
asyncandawaitthe result (or return the promise) - 2Nothing special is needed: Jest waits for all promises in the file
- 3Wrap the call in
setTimeout
Explanation
If the promise is neither awaited nor returned, the test ends before the assertion runs and passes falsely.