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

Explanation

Partial mocks keep tests closer to reality: only the inconvenient piece is replaced.

Further reading

Problem with a question

Testing · Middle · question 12

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