I am currently writing some tests in vitestand need to mock the fs module.
So inside the test file I did this:
vi.mock("node:fs", () => {
return {
readFileSync: vi.fn().mockReturnValue(`mocked value`),
existsSync: vi.fn().mockReturnValue(true),
}
})
This works, but I would like to create a file just for this code and import it then. However, vi.mockdoes not work when imported and executed inside a function (maybe I am doing something wrong here?)
So instead, I thought I would do something like this: import "./mockFS", hoping that the code does not get imported as a module. It does not work though.
Is there a way to achieve this, even though you normally really should not do something like this in "normal" code?