-
-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Easier way to use memfs in tests #29
Comments
You can also do
I would go with this one, just create
We could add
The vol.fromJSON(json, {
cwd: '/',
reset: true,
softlinks: { /* ... */ },
hardlinks: { /* ... */ },
fds: { /* ... open file state ... */ }
}); |
That’s inconvenient because I would need to export a method from this mock module that would recreate a volume.
I think that’s the most clear solution. I assume there is an easy way to implement that? I’d try ;-) |
Hmm, I'm thinking just resetting these properties should do the job: https://github.com/streamich/memfs/blob/master/src/volume.ts#L509-L539 Like: this.ino = 0;
this.inodes = {};
this.releasedInos = [];
// etc. And also create a new root node: this.root = new Link(this, null, '');
this.root.setNode(this.createNode(true)) |
BTW, could something like that do the job? // __mocks__/fs.js
const {createFsFromVolume, Volume} = require('memfs');
process.chdir('/');
const beforeEach = () => {
module.exports = createFsFromVolume(new Volume);
module.exports.__beforeEach = beforeEach;
};
exports.__beforeEach = beforeEach; And then: beforeEach(() => {
require('fs').__beforeEach();
}); |
I’ve tried something similar and it didn’t work — I assume all tests still use a link to the initial volume and just overwriting |
I’m trying 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);
});
}); |
I like the |
I’m trying to use memfs in tests. Use case is like this:
Mock
fs
with memfs: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:
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?
The text was updated successfully, but these errors were encountered: