Skip to content

test_runner: support mock file system #55902

Open

Description

@axetroy

What is the problem this feature will solve?

The current test requires writing files to a real disk when reading and writing files related to the file system, which is very unsafe.

After all, the file may remain due to test case failure and other reasons.

What is the feature you are proposing to solve the problem?

Under the mock namespace, APIs are exposed to provide a mock file system.

mock.fileSystem.enable({ });

And provide some options

e.g.

Override the file that defined by defaults

mock.fileSystem.enable({
  files: {
    '/path/to/file': new Uint8Array([0])
  }
});

// it will read the virtual file system first if it does exist
// if not found, then read the read the real file system
fs.readFileSync('/path/to/file')

Override the whole file system

mock.fileSystem.enable({
  files: {
    '/path/to/file': new Uint8Array([0])
  },
  override: true // override the whole file system
});

// This file is exists in the real file system
// But not define in the vitual file system, so it should throw an error
fs.readFileSync('/etc/proc')

What alternatives have you considered?

mock-fs

Activity

  1. added
    fsIssues and PRs related to file-system APIs and the fs module.
    test_runnerIssues and PRs related to the test runner subsystem.
    on Nov 18, 2024
  2. moved this from Awaiting Triage to Triaged in Node.js feature requestson Nov 18, 2024
  3. cjihrig commented on Mar 5, 2025

    @cjihrig
    Contributor

    It seems unlikely that anyone will pick this up since it has been open for nearly 5 months with no interactions. I think this would be a neat feature, but I'm not sure if it belongs in core (or if it does, it may be a better fit for fs than the test runner).

  4. github-actions commented on Sep 2, 2025

    @github-actions
    Contributor

    There has been no activity on this feature request for 5 months. To help maintain relevant open issues, please add the never-stale Issues and PRs exempt from automated stale handling. label or close this issue if it should be closed. If not, the issue will be automatically closed 6 months after the last non-automated comment.
    For more information on how the project manages feature requests, please consult the feature request management document.

  5. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Sep 2, 2025
  6. axetroy commented on Sep 2, 2025

    @axetroy
    Author

    bump

  7. removed
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Sep 3, 2025
  8. sozua commented on Jan 10, 2026

    @sozua

    Bringing back this discussion, @cjihrig. I'd like to try helping on this, but wanted to clarify some things first.

    1: On whether this belongs in the test runner or fs: the test runner already owns mock.timers and module mocking. Would mock.fs fit that pattern, or is there a reason to keep it closer to the fs subsystem?

    2: The fs API surface is large, so full coverage isn't very practical for a first pass. A starting point could be basic file I/O and directory operations. Enough to let tests define virtual files and avoid writing to disk. Unmocked paths could fall back to real fs, or throw if the user wants full isolation.

    Does that seem like a reasonable scope, or should the first iteration be narrower/broader?

  9. cjihrig commented on Jan 10, 2026

    @cjihrig
    Contributor

    @sozua I'm going to ping @nodejs/test_runner about your comment above ^. I personally don't have an opinion on it.

  10. MoLow commented on Jan 15, 2026

    @MoLow
    Member

    Id add it as mock.fs, and mark it as experimental, to allow it to be gradually implemented

  11. ljharb commented on Jan 15, 2026

    @ljharb
    SponsorMember

    I would suggest keeping it behind a flag - not just marked as experimental (which is largely useless, because if it gets sufficient adoption it can't be removed anyways) - until it has full API coverage.

  12. github-actions commented on Jul 20, 2026

    @github-actions
    Contributor

    This issue has been marked as stale due to 90 days of inactivity.
    It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

  13. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Jul 20, 2026
  14. axetroy commented on Jul 20, 2026

    @axetroy
    Author

    bump

  15. pmarchini commented on Jul 20, 2026

    @pmarchini
    Member

    We might want to take a look at #63115 👀

  16. axetroy commented on Jul 20, 2026

    @axetroy
    Author

    We might want to take a look at #63115 👀

    This is a virtual file system, and it doesn't seem to be able to meet this requirement.

    Because our actual code will definitely use node:fs to operate on files.

    It wouldn't make sense to modify the source code to replace the node:fs module with node:vfs just for the sake of testing.

  17. pmarchini commented on Jul 20, 2026

    @pmarchini
    Member

    No I agree, what I'm saying is that we can use vfs as a base for the fs mock system!

  18. removed
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Jul 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues requesting new Node.js features.fsIssues and PRs related to file-system APIs and the fs module.test_runnerIssues and PRs related to the test runner subsystem.

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions