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");
});

Explanation

If the promise is neither awaited nor returned, the test ends before the assertion runs and passes falsely.

Further reading

Problem with a question

Testing · Junior · question 24

Test yourself

Take the Testing · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz