Testing · Middle · Question 12 of 75
How do you mock only one export of a module and keep the rest real?
vi.mock("./utils", async (importOriginal) => ({
...(await importOriginal()),
now: () => 0,
}));- ✓Correct answer: As shown: spread the original module and override one export (in Jest:
jest.requireActual) - 2Impossible - a module is mocked entirely
- 3Use
vi.spyOn("./utils", "now")
Explanation
Partial mocks keep tests closer to reality: only the inconvenient piece is replaced.