I'm having trouble resetting an imported file completely after each test. I believe that using vi.mock
should mimic the original contents of my imported file, but it doesn't seem to be working when I try to modify the file during the tests. Here are the relevant files:
src/
lib/
index.ts
index.test.ts
// index.js
const settings = { value: '' }
export function setValue(val) {
settings.value = val;
}
export function getValue() {
return settings.value;
}
// index.test.js
import { setValue, getValue } from "./index.js";
import { afterEach, describe, expect, it, vi } from "vitest";
vi.mock("./index.js");
describe("setValue", () => {
afterEach(() => {
vi.clearAllMocks();
vi.resetAllMocks();
});
it("test1", () => {
setValue('en')
expect(getValue()).toBe('en'); // getting undefined instead
});
});