question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: Using mock-fs in beforeEach hooks breaks console.log()s

See original GitHub issue

For some reason, using mock-fs in beforeEach() hooks causes all calls to console.log in it() functions to throw a ENOENT, no such file or directory '/path/to/project/node_modules/callsites' error.

const mock = require("mock-fs");

describe("tests", () => {
  beforeEach(() => {
    mock({
      "/foo/bar": {}
    });
  });

  afterEach(() => {
    mock.restore();
  });
  it("should fail", () => {
    console.log(`This console.log will cause an error`);
  });
});

envinfo:

  System:
    OS: macOS Sierra 10.12.6
    CPU: x64 Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
  Binaries:
    Node: 8.4.0
    Yarn: 1.3.2
    npm: 5.6.0
  npmPackages:
    jest:
      wanted: ^22.4.2
      installed: 22.4.2

Reproduction repo: https://github.com/AriaMinaei/jest-mock-fs-bug

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
nknappcommented, Mar 27, 2018

Seems to me that calling console.log just before calling mock-fs is a workaround. https://github.com/nknapp/jest-mock-fs-bug

3reactions
ColChcommented, Jun 6, 2019

happends on most recent jest@24

Read more comments on GitHub >

github_iconTop Results From Across the Web

mock-fs test fails in jest when console.log - Stack Overflow
For some strange reason when I try to console.log within a jest test using mock-fs it fails, I have no clue why this...
Read more >
How to Test a Node.js Command-Line Tool | by Yeyan
For example, with the beforeEach hook, the code is mocked before each test case is run, and after it is finished, it is...
Read more >
@gvhinks/search-github NPM | npm.io
Check @gvhinks/search-github 1.0.1 package - Last release 1.0.1 with GPL-3.0 ... 2 months │ Bug: Using mock-fs in beforeEach hooks breaks console.log()s ......
Read more >
mock-fs - npm
mock-fs. The mock-fs module allows Node's built-in fs module to be backed temporarily by an in-memory, mock file system.
Read more >
@jest/fake-timers | Yarn - Package Manager
npm version Jest is released under the MIT license. Follow on Twitter. GitHub CI Status Coverage Status. Gitpod ready-to-code ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found