tschaub / mock-fs

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

Mixing real fs calls with mocks hangs process. #264

Closed ryan-roemer closed 5 years ago

ryan-roemer commented 5 years ago

First of all, fantastic library!!!. I've been putting off my CI upgrades to node 10.5+ because of mock-fs not supporting them yet. Seeing as that great work is merged, I attempted to upgrade, but hit the issue that I've honed down to this reproduction:

// FILE: temp-test.js
// Reproduction.
const { readFile } = require("fs");
const mock = require("mock-fs");
const { promisify } = require("util");

// Convert to promises for readability. Works the same with callbacks.
const readFileP = promisify(readFile);

const { DO_REAL_READFILE } = process.env;

(async () => {
  if (DO_REAL_READFILE === "true") {
      // TODO HERE: Merely adding a **real** `readFile` of the any file before
    // errors the latter one. Comment this out and test passes.
    // Uncomment and test hangs.
    const buf1 = await readFileP("package.json");
    console.log("TODO HERE 001", { data: buf1.toString() });
  }

  // Also, if no `mock()`, the two real readFile calls also succeed.
  mock({
    "package.json": "BAD_NOT_JSON",
  });

  const buf2 = await readFileP("package.json");
  console.log("TODO HERE 002", { data: buf2.toString() });
})();

The issue is that doing a mix of a real readFile and then a mock() call and then another readFile (the calls don't have to match the same file) results in the mocked, second readFile call hanging the process.

And more specifically, if that file exists as a mock for "package.json", the process hangs. If instead, mock() is used, there is an ENOENT thrown as expected because the mocked file isn't found.

Usage:

$ nvm use 8
Now using node v8.11.3 (npm v5.6.0)

$ node --version
v8.11.3

$ node temp-test.js 
# doesn't hang

$ DO_REAL_READFILE=true node temp-test.js 
# doesn't hang

$ nvm use 10
Now using node v10.13.0 (npm v6.4.1)

$ node --version
v10.13.0

$ node temp-test.js
# doesn't hang

$ DO_REAL_READFILE=true node temp-test.js
# HERE -- THIS HANGS THE PROCESS after 001 log before 002 log.

I'm not sure exactly what's up internally, but hopefully this is pretty straightforward reproduction. Let me know if I can provide any more info.

Apologies if there is something obvious / configuration-wise / etc. that I'm missing as to why this isn't working for me...

Thanks!

tschaub commented 5 years ago

@ryan-roemer please try out mock-fs@4.9.0 for the fix (thanks @huochunpeng).

ryan-roemer commented 5 years ago

@tschaub -- Works like a charm! My (very heavily leveraging mock-fs) tests now pass in 8, 10, 11 for Travis and Appveyor: https://github.com/FormidableLabs/inspectpack/pull/113

Thanks for the fantastic work @huochunpeng !

🎉 🙏 🎉