Title: test_runner: add experimental `mock.fs` API by sozua · Pull Request #61468 · nodejs/node · GitHub
Open Graph Title: test_runner: add experimental `mock.fs` API by sozua · Pull Request #61468 · nodejs/node
X Title: test_runner: add experimental `mock.fs` API by sozua · Pull Request #61468 · nodejs/node
Description: Add experimental mock.fs API to the test runner for mocking file system operations in tests. This allows tests to simulate file operations without actually reading from or writing to disk. Note: There is an existing draft PR #59194 by @joaoGabriel55 working on this feature, though it appears to have stalled (last updated August 2025). This implementation takes a different approach based on maintainer feedback in the issue discussion: Uses mock.fs naming as suggested by @MoLow Gated behind --experimental-test-fs-mocks flag as suggested by @ljharb Includes callback and promise variants, not just sync methods Features Virtual file system with string, Buffer, or Uint8Array content Supports sync, callback, and promise variants of fs APIs Mocked APIs: readFile, writeFile, appendFile, stat, lstat, access, exists, unlink, mkdir, rmdir, readdir Isolation mode (isolate: true) to completely block real filesystem access Selective API mocking via the apis option Write operations always go to virtual fs (prevents accidental disk writes) Symbol.dispose support for automatic cleanup Examples Basic usage import { test } from 'node:test'; import fs from 'node:fs'; import assert from 'node:assert'; test('mock file system', (t) => { t.mock.fs.enable({ files: { '/virtual/config.json': '{"key": "value"}', }, }); const config = JSON.parse(fs.readFileSync('/virtual/config.json', 'utf8')); assert.strictEqual(config.key, 'value'); }); Isolation mode test('complete isolation from real filesystem', (t) => { t.mock.fs.enable({ files: { '/virtual/only.txt': 'isolated content', }, isolate: true, }); // Virtual file works assert.strictEqual(fs.readFileSync('/virtual/only.txt', 'utf8'), 'isolated content'); // Real files are blocked assert.throws(() => fs.readFileSync('/etc/passwd'), { code: 'ENOENT' }); }); Write operations test('writes always go to virtual fs', (t) => { t.mock.fs.enable({ files: {} }); // Write to virtual fs fs.writeFileSync('/tmp/test.txt', 'safe write'); // Read it back assert.strictEqual(fs.readFileSync('/tmp/test.txt', 'utf8'), 'safe write'); // After test ends, nothing written to real /tmp }); Async/promises import fsPromises from 'node:fs/promises'; test('works with promises', async (t) => { t.mock.fs.enable({ files: { '/data/file.txt': 'async content' }, }); const content = await fsPromises.readFile('/data/file.txt', 'utf8'); assert.strictEqual(content, 'async content'); }); Directory operations test('directory operations', (t) => { t.mock.fs.enable({ files: { '/app/src/index.js': 'console.log("hello")', '/app/src/utils.js': 'export default {}', }, }); const entries = fs.readdirSync('/app/src'); assert.deepStrictEqual(entries.sort(), ['index.js', 'utils.js']); }); Next Steps This PR provides an initial set of commonly-used fs operations. Future iterations could add file operations like rename, copyFile, rm, and truncate, as well as symlink support (symlink, readlink, link). Refs: #55902
Open Graph Description: Add experimental mock.fs API to the test runner for mocking file system operations in tests. This allows tests to simulate file operations without actually reading from or writing to disk. Note: Th...
X Description: Add experimental mock.fs API to the test runner for mocking file system operations in tests. This allows tests to simulate file operations without actually reading from or writing to disk. Note: Th...
Opengraph URL: https://github.com/nodejs/node/pull/61468
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:388fa877-d608-f258-cbff-4ee9be471b66 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | A2C0:951F5:17FE288:22D3B19:6A4CECA5 |
| html-safe-nonce | 490fda920eee53175f1bc510c6bb9c68d02ac46dd83a49c9d26bccc7f60a59d2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMkMwOjk1MUY1OjE3RkUyODg6MjJEM0IxOTo2QTRDRUNBNSIsInZpc2l0b3JfaWQiOiI0MDEyOTcxMDMyMzAyMTIwMTAxIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 1f31b8facaa35946db542eece9d65213edc6a166b46e29eb590c61ee7fb11a85 |
| hovercard-subject-tag | pull_request:3197963229 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/nodejs/node/pull/61468/files |
| twitter:image | https://avatars.githubusercontent.com/u/41171734?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/41171734?s=400&v=4 |
| og:image:alt | Add experimental mock.fs API to the test runner for mocking file system operations in tests. This allows tests to simulate file operations without actually reading from or writing to disk. Note: Th... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 299b43bca6e02ad35197ffeba30d2466846d5fb02ab96fbced5b5e6cec589fb8 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/nodejs/node git https://github.com/nodejs/node.git |
| octolytics-dimension-user_id | 9950313 |
| octolytics-dimension-user_login | nodejs |
| octolytics-dimension-repository_id | 27193779 |
| octolytics-dimension-repository_nwo | nodejs/node |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27193779 |
| octolytics-dimension-repository_network_root_nwo | nodejs/node |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | efec6750a5afcaeaf5dfcf629f404cf98c8371e3 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width