Skip to content

Instantly share code, notes, and snippets.

@theKashey
Last active May 21, 2020 19:35
Show Gist options
  • Select an option

  • Save theKashey/5938e117f07924691b23a70d7f516076 to your computer and use it in GitHub Desktop.

Select an option

Save theKashey/5938e117f07924691b23a70d7f516076 to your computer and use it in GitHub Desktop.
import test from 'ava';
import sinon from 'sinon';
// ^ uses a real FS
// nothing before would be mocked
import rewiremock from 'rewiremock/node';
import mockfs from 'mock-fs';
import mountfs from 'mountfs';
import fs from 'fs';
rewiremock('fs').mockThrough();
mockfs.patchInPlace();
// ^ mountfs will "patch" not real FS, but a mock :P
fs.mount('/some/test/directory/', mockfs.fs({
'/some/test/directory': {
'some-file.txt': 'mock contents'
}
});
@chaorace

Copy link
Copy Markdown

This worked for me in a nodejs module after a small few adjustments:

import test from 'ava';

// Nothing before is mocked
import rewiremock from 'rewiremock/node.js';

import mockfs from 'mock-fs';
import mountfs from 'mountfs';
import fs from 'fs';

rewiremock('fs').mockThrough();

mountfs.patchInPlace();

fs.mount('/topdir/', mockfs({
    '/topdir': {
        'foobar': {
            'baz/barney': {},
            'foo.js': 'console.log("foobar")',
        },
        'foobaz/bar.json': '{}',
    },
}));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment