Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mixing real fs calls with mocks hangs process. #264

Closed
ryan-roemer opened this issue Mar 30, 2019 · 2 comments · Fixed by #265
Closed

Mixing real fs calls with mocks hangs process. #264

ryan-roemer opened this issue Mar 30, 2019 · 2 comments · Fixed by #265

Comments

@ryan-roemer
Copy link

ryan-roemer commented Mar 30, 2019

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
Copy link
Owner

tschaub commented Apr 22, 2019

@ryan-roemer please try out [email protected] for the fix (thanks @huochunpeng).

@ryan-roemer
Copy link
Author

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

Thanks for the fantastic work @huochunpeng !

🎉 🙏 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants