Testing · Senior · Question 25 of 75

How do you get a fresh copy of a module with module-level state in each test?

beforeEach(() => vi.resetModules());
test("...", async () => {
  const { counter } = await import("./counter");
});

Explanation

Static imports are evaluated once per file; only a dynamic import after reset gets a new instance.

Further reading

Problem with a question

Testing · Senior · question 25

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