nodejs / nodejs/node

test_runner: support mock file system

Open
#55,902 12 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature request fs test_runner
Dominant language
JavaScript
Stars
122k
Forks
37.3k
Avg merge
4d 2h
Merged PRs (30d)
283

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the test_runner mock namespace and the file-system APIs described in the issue. Define how mock.fileSystem.enable handles configured files, fallback to the real file system, and override mode; done means these behaviors are exposed and covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
operating-systems, testing-qa
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.