Skip to content

Mixing real fs calls with mocks hangs process. #264

Closed
@ryan-roemer

Description

@ryan-roemer

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions