-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mock fs #110
Comments
What do you mean by that? |
Just an idea from Peeky, not sure if we want it |
This seems like the most appropriate issue to share my struggle with mocking fs.
import { readFileSync } from "fs";
export const getEngines = () => {
return readFileSync("engines.ts");
};
import { expect, test, vi } from "vitest";
import { getEngines } from "../engines.js";
test("works", async () => {
// Arrange
vi.mock("node:fs", async () => {
return {
...(await vi.importActual<typeof import("node:fs")>("node:fs")),
readFileSync: vi.fn().mockReturnValue(`mocked value`),
};
});
const { readFileSync } = await import("node:fs");
console.log(readFileSync);
// Act
const result = getEngines();
// Assert
expect(result).toBe("mocked value"); // works
expect(readFileSync).toHaveBeenCalled(); // doesn't
}); As soon as I changed But still, quite an interesting artifact that mocking was actually kinda working even with an inconsistent module name. |
This is something I think a lot of teams could benefit from too @antfu and @sheremet-va. I can see it being really useful for CLIs, scripts, and server code. Mocking |
The team thinks it should not be part of the core because it adds more maintenance burden that we don't want to take. Additionally, the use case is not popular enough to ship this to everyone. Instead, we are adding a section "Mocking File System" to our documentation in #6021 recommending the usage of |
No description provided.
The text was updated successfully, but these errors were encountered: