test_runner: add mock file system API - #65798
Conversation
Add t.mock.fs(): an in-memory mock file system backed by a mounted virtual file system. The mock lives at a reserved mount point exposed as mountPoint, so it never shadows real files, and it is unmounted automatically when the test finishes. Signed-off-by: Matteo Collina <[email protected]>
|
Review requested:
|
| Adds a file to the mock file system. Missing parent directories are | ||
| created automatically. | ||
|
|
||
| ### `mockFs.existsSync(path)` |
There was a problem hiding this comment.
I don't understand why we'd want/need these separate APIs (like existsSync/addRirectory etc) the user can just call mockFs.vfs.mkdirSync can't they?
| shadows real files or directories. Join it with relative paths to access | ||
| the mock's files through the `node:fs` APIs. | ||
|
|
||
| ### `mockFs.restore()` |
There was a problem hiding this comment.
For restore to make sense here IMO the vfs would need to actually "take over" something like other mocks. I don't understand when I'd ever do this.
OTOH if mockFs could intercept/take over calls (i.e. like mock.module) it would make sense.
This is cool but I don't understand why I'd use this over mock.module("node:fs", { exports: new VirtualFileSystem() })
benjamingr
left a comment
There was a problem hiding this comment.
I'm approving since the code itself looks fine and this is tagged early development and iteration is fine - but I'm not convinced this has a benefit over mock.module + VirtualFileSystem and I'm wary of adding more ways to e.g. create a file or directory since the current ones work with VirtualFileSystem as is
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #65798 +/- ##
==========================================
+ Coverage 90.05% 90.13% +0.08%
==========================================
Files 769 769
Lines 261396 261765 +369
Branches 49631 49690 +59
==========================================
+ Hits 235399 235946 +547
+ Misses 17034 16852 -182
- Partials 8963 8967 +4
🚀 New features to boost your workflow:
|
Add
t.mock.fs(): an in-memory mock file system for the test runner, backed by a mountedVirtualFileSystem.The mock is mounted at a reserved mount point (
${os.devNull}/vfs/<n>) assigned when it is created and exposed asmockFs.mountPoint, so it never shadows real files or directories. Files are accessible through the regularnode:fsAPIs and can be loaded withrequire()andimportvia the module loader integration.The returned
MockFSContextexposesmountPoint,vfs,addFile(),addDirectory(),existsSync(), andrestore(). Because mount points are assigned rather than chosen,addFile()/addDirectory()return the absolute path of what they created. The mock is unmounted automatically when the associated test finishes, like the otherMockTrackerAPIs.This adapts the mock fs concept originally proposed for the shadowing VFS design (where a
prefixoption mounted over real paths) to the non-shadowing implementation that landed in #63653.🤖 Generated with Claude Code
https://claude.ai/code/session_01BuuR21ULqZEHoFmqWCnoyx