I got two difference cases when using vitest to write mocks:
case 1:
const mockedMethod = vi.fn();
vi.mock('./path/to/module.js', () => ({
originalMethod: mockedMethod,
}));
case 2:
const mockedMethod = [1, 2, 3]; // Defined before vi.mock()
vi.mock('./path/to/module.js', () => ({
originalMethod: mockedMethod,
}));
I wonder why for case 1, it will raise error to say mockedMethod
is undefined
.
But for case 2, it will pass.