tschaub / mock-fs

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

Question: does accessSync not work with this library? #248

Closed begin-again closed 6 years ago

begin-again commented 6 years ago

Does accessSync not work with this library? The below assert is returning undefined AssertionError [ERR_ASSERTION]: undefined == true

const fs = require('fs');
const fakeFS = require('mock-fs');
const assert = require('assert')

fakeFS({
    './fakeFile': { }
});
assert.ok(fs.accessSync('./fakeFile', fs.constants.F_OK));
maxwellgerber commented 6 years ago

It works - accessSync does not have a return value. It throws an error if the flags are not compatible. See: https://nodejs.org/api/fs.html#fs_fs_accesssync_path_mode

Try this instead:

const fs = require('fs');
const fakeFS = require('mock-fs');
const assert = require('assert')

fakeFS({
    './fakeFile': { }
});
assert.doesNotThrow(() => fs.accessSync('./fakeFile', fs.constants.F_OK));
assert.throws(() => fs.accessSync('./bad/file/path', fs.constants.F_OK));
tschaub commented 6 years ago

@maxwellgerber's answer looks right to me. Please reopen if you find the mock-fs behavior different than regular fs behavior.