tschaub / mock-fs

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

ENOENT, no such file or directory when using readAsync #258

Closed YonatanKra closed 5 years ago

YonatanKra commented 5 years ago

Hi, Here's my code:

// inside files.js
readConfig: function(root = process.cwd()) {
        console.log('root: ', root);
        return JSON.parse(fs.readFileSync(path.join(root, 'my.conf.json'), 'utf8'));
}

Here's my spec:

const config = 'test config';
const CONFIG_ROOT = 'configRoot';

describe(`files`, () => {

    beforeAll(() => {
        mockfs({
           [CONFIG_ROOT]: {
                "gfuncs.conf.json": JSON.stringify(config)
            }
        }})
    });

    afterAll(() => {
        mockfs.restore()
    });

    describe(`readConfig`, () => {
        it(`should return the configuration as a JSON`, () => {
            expect(files.readConfig(CONFIG_ROOT)).toEqual(config);
        });
    });
});

I get the following error: ENOENT, no such file or directory '\\?\C:\configRoot\my.conf.json'

Any idea how to get over it? Thanks!

YonatanKra commented 5 years ago

Got it! Just found out these lines in the documentation:

createCwd - boolean Create a directory for process.cwd(). This is true by default.
createTmp - boolean Create a directory for os.tmpdir(). This is true by default.

So there's no need to set the root, hence my beforeAll should be like this:

mockfs({
                "gfuncs.conf.json": JSON.stringify(config)
        })