Testing · Middle · Question 11 of 75

Why does this mock factory fail?

const user = { name: "Ann" };
vi.mock("./api", () => ({ getUser: () => user }));

Explanation

const user = vi.hoisted(() => ({ name: "Ann" })) is hoisted together with the mock. Jest allows only variables prefixed with mock.

Further reading

Problem with a question

Testing · Middle · question 11

Test yourself

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

Take this quiz