tschaub / mock-fs

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

Mocking fails under node 14.4.0? #299

Closed paul23-git closed 3 years ago

paul23-git commented 4 years ago

Well as per title, it seems no mocking occurs under nodejs 14.4.0. Using jest to test the following simple test "fails":

import mock = require("mock-fs");
import {existsSync} from "fs";
describe("config-loader-loadJSON", function() {

    const content = {
        test: 123,
        sub: {
            a: '123'
        }
    };

    mock({
        'textFile.txt': 'This is file content',
        'jsonFile.json': JSON.stringify(content),
    });
    it("Should load", async function() {
        expect(existsSync("jsonFile.json")).toBeTruthy();
    });
});
3cp commented 3 years ago

That's not legit jest code. Following works on nodejs 14.4.0.

const mock = require("mock-fs");
const {existsSync} = require("fs");

describe("config-loader-loadJSON", function() {

    beforeEach(function() {
        const content = {
            test: 123,
            sub: {
                a: '123'
            }
        };

        mock({
            'textFile.txt': 'This is file content',
            'jsonFile.json': JSON.stringify(content),
        });
    });

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

    it("Should load", async function() {
        expect(existsSync("jsonFile.json")).toBeTruthy();
    });
});