Easier way to use memfs in tests
See original GitHub issueI’m trying to use memfs in tests. Use case is like this:
-
Mock
fs
with memfs:// __mocks__/fs.js process.chdir('/'); module.exports = require('memfs').fs;
-
Load the initial state from JSON using
fromJSON
method. -
Run methods I’m testing.
-
Inspect the resulting JSON from
toJSON
.
The problem is that every time I call fromJSON
it modifies the volume, keeping all existing files from previous tests.
I see two possible solutions:
- Recreate a memfs volume before each test. Seems a bit too complex.
rm -rf /
after each tests. But it seems scary — what if I forget to calljest.mock('fs')
? ;-|
As a perfect solution I see fromJSON
that can remove all existing files before loading JSON. Could be a new parameter or a new method.
What do you think?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Testing filesystem in Node.js: Please use memfs | Nerd For Tech
A clean, flexible and straightforward approach to testing filesystem related code in Node.js using memfs library. Explained why you should ...
Read more >Node.js testing: Using a virtual filesystem as a mock
This post shows an alternative method using unionfs and memfs. The advantage of this method is that it allows you to overlay your...
Read more >A simple way how to use memfs with Jest for testing filesys…
A simple way how to use memfs with Jest for testing filesystem: https://github.com/sapegin/mrm-core/blob/master/__mocks__/fs.js. Leave your comment.
Read more >How can I mock the fs module using memfs in TypeScript?
I am currently trying to mock out the fs module by using mocks in Jest and replacing fs functions with memfs methods. I...
Read more >Speed up your tests with an in-memory filesystem | 8th Light
Well, if your tests use a database, then your database probably writes stuff to a physical disk somewhere—and we all know how fast...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m trying
.reset()
and test cases look really nice:I like the
.toJSON()
with.toMatchSnapshot()
.