Skip to content
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

Closed
antfu opened this issue Dec 13, 2021 · 5 comments · Fixed by #6021
Closed

Mock fs #110

antfu opened this issue Dec 13, 2021 · 5 comments · Fixed by #6021
Labels
enhancement New feature or request p2-to-be-discussed Enhancement under consideration (priority) pr welcome

Comments

@antfu
Copy link
Member

antfu commented Dec 13, 2021

No description provided.

@antfu antfu added the enhancement New feature or request label Dec 13, 2021
@sheremet-va
Copy link
Member

What do you mean by that?

@antfu
Copy link
Member Author

antfu commented Dec 20, 2021

image
https://peeky.netlify.app/

Just an idea from Peeky, not sure if we want it

@Maxim-Mazurok
Copy link
Contributor

Maxim-Mazurok commented Jun 27, 2022

This seems like the most appropriate issue to share my struggle with mocking fs.
TL;DR I can mock it with inconsistent module name, but can't keep track of calls.

engines.ts

import { readFileSync } from "fs";

export const getEngines = () => {
  return readFileSync("engines.ts");
};

mock.vispec.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 import { readFileSync } from "fs"; to import { readFileSync } from "node:fs"; in engines.ts - it started working.

But still, quite an interesting artifact that mocking was actually kinda working even with an inconsistent module name.

@mrmckeb
Copy link

mrmckeb commented May 30, 2023

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 fs is a fine option, but isn't as powerful/intuitive as a mocked file system.

@sheremet-va sheremet-va added the p2-to-be-discussed Enhancement under consideration (priority) label Feb 16, 2024
@sheremet-va sheremet-va moved this to P2 - 5 in Team Board Mar 28, 2024
@sheremet-va sheremet-va moved this from P2 - 5 to Rejected in Team Board Apr 11, 2024
@sheremet-va
Copy link
Member

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 memfs.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request p2-to-be-discussed Enhancement under consideration (priority) pr welcome
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants