tschaub / mock-fs

Configurable mock for the fs module
https://npmjs.org/package/mock-fs
Other
906 stars 86 forks source link

Feature request: Allow incremental population of mocked fs #353

Open BadIdeaException opened 2 years ago

BadIdeaException commented 2 years ago

Before I say anything else, let me say thank you for a job well done! mock-fs is awesome, it's easy to use and makes testing file-system related stuff much less painless and faster than dealing with the real thing.

To make it even more painless and easy to use: It would be very nice if there was some way to populate the mocked file system incrementally. This would allow to set up some common files in before and beforeEach, then add test-specific stuff within the actual test. Like this:

beforeEach(function() {
    // Pre-populate the file-system with some data that is common to all following tests:
    mockfs({
        foo: {
            bar: 'baz'
        }
    })
});

it('should do complicated things correctly', function() {
    mockfs({
        'foo/qux': 'quux'
    });
    expect(complicated_things()).to.be.true;
});

it('should fail if qux is empty', function() {
    mockfs({
        'foo/qux': ''
    });
    expect(complicated_things()).to.be.false;
});
3cp commented 2 years ago

Once mockfs is inited, for now, you can use plain fs calls to populate the files.

BadIdeaException commented 2 years ago

True. The other way would still seem like a cleaner API to me, but I can see how this would probably not be a high-priority thing to implement. Thanks for the quick reply.

3cp commented 2 years ago

Probably add additional api like mockfs.merge or mockfs.patch. We don't want the existing api surprises user if user forgot to call restore between two mockfs calls.

BadIdeaException commented 2 years ago

Yeah, I like that idea (although anyone not restore()ing between tests is probably already in for a nasty surprise anyways).