Testing · Senior · Question 63 of 75

How do you mock a class constructor with Vitest/Jest?

vi.mock("./Logger", () => ({
  Logger: vi.fn().mockImplementation(() => ({ log: vi.fn() })),
}));

Explanation

Logger.mock.instances / mock.results give access to the created instances.

Further reading

Problem with a question

Testing · Senior · question 63

Test yourself

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

Take this quiz