Testing · Middle · Question 11 of 75
Why does this mock factory fail?
const user = { name: "Ann" };
vi.mock("./api", () => ({ getUser: () => user }));- ✓Correct answer: The factory is hoisted above the
const, souserisn't initialized yet; usevi.hoistedor define data inside the factory - 2Factories can't return objects
- 3
vi.mockneeds an absolute path
Explanation
const user = vi.hoisted(() => ({ name: "Ann" })) is hoisted together with the mock. Jest allows only variables prefixed with mock.