tschaub / mock-fs

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

mkdtemp creates real directory #205

Closed CzBuCHi closed 7 years ago

CzBuCHi commented 7 years ago

not sure what going on but code bellow will create new directory in cwd: (expected behavior is that it will create temp dir in mocked fs..)

describe('pfs', function () {
    beforeEach(function () {
        mock({
            'file.txt': 'fred',
            'folder': {}
        });
    });

    afterEach(function () {
        mock.restore();
    });

    // just test, that my promise wrapper calls fs.mkdtemp with correct param...
    it('mkdtemp(prefix: string)', function () {
        const spy = sinon.spy(fs, 'mkdtemp');
        return pfs.mkdtemp('prefix')
            .then(name => {
                spy.restore();
                sinon.assert.calledOnce(spy);
                const args = spy.getCall(0).args;
                expect(args).to.deep.equal(['prefix']);
            });
    });
});

module pfs {
  export function mkdtemp(prefix: string) {
    return new Promise<string>((resolve, reject) => fs.mkdtemp(prefix, (err, folder) => { 
      if(err) reject(err); 
      else resolve(folder);
    });
  }
}
tschaub commented 7 years ago

Thanks for the report @CzBuCHi. mkdtemp is missing from the mock binding. It shouldn't be too much effort to add.

tschaub commented 7 years ago

fs.mkdtemp() and fs.mkdtempSync() support added in mock-fs@4.3.