question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Easier way to use memfs in tests

See original GitHub issue

I’m trying to use memfs in tests. Use case is like this:

  1. Mock fs with memfs:

    // __mocks__/fs.js
    process.chdir('/');
    module.exports = require('memfs').fs;
    
  2. Load the initial state from JSON using fromJSON method.

  3. Run methods I’m testing.

  4. 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:

  1. Recreate a memfs volume before each test. Seems a bit too complex.
  2. rm -rf / after each tests. But it seems scary — what if I forget to call jest.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:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
sapegincommented, Aug 30, 2017

I’m trying .reset() and test cases look really nice:

jest.mock('fs');

const { vol } = require('memfs');
const { copyFiles } = require('../fs');

afterEach(vol.reset.bind(vol));

describe('copyFiles()', () => {
	it('should copy a file', () => {
		vol.fromJSON({ 'tmpl/a': 'pizza' });

		copyFiles('tmpl', 'a');

		expect(vol.toJSON()).toMatchSnapshot();
	});

	it.skip('should copy multiple files', () => {
		vol.fromJSON({ 'tmpl/a': 'pizza', 'tmpl/b': 'coffee' });

		copyFiles('tmpl', ['a', 'b']);

		expect(vol.toJSON()).toMatchSnapshot();
	});

	it.skip('should not overwrite a file if  overwrite=false', () => {
		const json = { 'tmpl/a': 'pizza', a: 'pizza' };
		vol.fromJSON(json);

		copyFiles('tmpl', 'a', { overwrite: false });

		expect(vol.toJSON()).toEqual(json);
	});
});
1reaction
streamichcommented, Aug 30, 2017

I like the .toJSON() with .toMatchSnapshot().

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found