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");
});- ✓Correct answer: Reset the module registry and import the module dynamically inside the test (Jest:
jest.resetModules/isolateModules) - 2Call
delete require.cachein ESM - 3It's impossible - modules are singletons
Explanation
Static imports are evaluated once per file; only a dynamic import after reset gets a new instance.