On mocking fs with metro-memory-fs
January 2, 2020
If you've done your share of testing Node, you've probably found yourself wanting to mock the file-system, particularly the fs
core module. I've had mixed success with this historically; generally it becomes an exercise in GitHub Issue and StackOverflow spelunking.
In my last foray, I stumbled upon this issue. After reading through it I found that someone suggested the in-memory fs
used by the React Native bundler Metro for its automated tests. As luck would have it, that module is already published as metro-memory-fs
. Sweet!
Getting Started With metro-memory-fs
There's not much documentation for metro-memory-fs
(read: none 😅), but fortunately it's pretty straightforward! I'm going to assume that you are using jest
to write your unit tests from here on out.
We can start by installing it:
In your tests, you can initialize the mock like this:
Now that we're setup we can start acting against our mocked fs
instance.
Note: cwd
configuration
If you encounter an error similar to this:
Then you might have to configure the cwd
option like this:
This was enough for me to fix the issue and continue writing my tests.
Writing Tests
Let's assume that we've got a function which writes to a file called test.txt
. Problem is, we don't want to have to manually delete the file after our tests are done. Which is probably why we stumbled on this article... 🤔. Now that we've got our mock setup with metro-memory-fs
this should be easy!
Here's our test setup:
Alright, looking good. Let's add a test case...
That's it! By mocking fs
with metro-memory-fs
we are able to write tests against the file-system with minimal ceremony. 🎉
Reducing Boilerplate
If you anticipate having to lean on metro-memory-fs
for many of your tests you could specify the mock in one of your setupFiles
, or create an importable helper like this:
That's A Wrap
Mocking fs
doesn't have to be hard, I'm pretty glad I stumbled upon metro-memory-fs
. Hopefully this post can serve as simple documentation for the package and save you a few minutes if you decide to implement metro-memory-fs
in your own project! I'll do my best to update this post where necessary as I continue to take advantage of the package in my own test suites.
Credits
- The package itself (https://www.npmjs.com/package/metro-memory-fs)
- The issue I linked above was what initially pointed me towards
metro-memory-fs
and provided the mocking setup (https://github.com/facebook/jest/issues/3154) - The test files for
metro-memory-fs
were really helpful for understanding how to use the module (https://github.com/facebook/metro/blob/master/packages/metro-memory-fs/src/__tests__/index-test.js)