tschaub / mock-fs

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

Should readFileSync(), statSync() work? #385

Closed ddziara closed 6 months ago

ddziara commented 6 months ago

Hello,

node ver: v20.9.0

the following code crashes (no such file) for readFileSync(). statSync() has the same problem.

import mock from 'mock-fs';
// import fs from "fs";
import fs from "fs/promises";

describe("Dummy test", () => {
    beforeEach(() => {
        mock({
                '/index.md': '# Hello world!',
        });
    });

    afterEach(() => {
        mock.restore();
    });

/*    
    test("test1", () => {
        const buf = fs.readFileSync(`/index.md`);                        // no such file

        console.log(buf.toString());
    });

    test("test2", (done) => {
        fs.readFile(`/index.md`, (err, data) => {
            console.log(data.toString());
            done();
        });
    });
*/

    test("test3", async () => {
        const buf = await fs.readFile(`/index.md`)

        console.log(buf.toString());
    });
});
tschaub commented 6 months ago

I'll close this as a duplicate of #380. If it looks like something else to you, feel free to reopen. I see that #381 includes some work toward a solution.