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

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

Closed
AriaMinaei opened this issue Mar 14, 2018 · 10 comments
Closed

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

AriaMinaei opened this issue Mar 14, 2018 · 10 comments
Labels

Comments

@AriaMinaei
Copy link

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

@ranyitz
Copy link
Contributor

ranyitz commented Mar 19, 2018

I'll try to shed some light on this issue.

callsites is used in the buffered console under the write method, to understand the origin of the log and provide a better experience.

https://github.com/facebook/jest/blob/124067e7d662364d40fc662ef54bfddd798c57c4/packages/jest-util/src/buffered_console.js#L24

https://github.com/facebook/jest/blob/124067e7d662364d40fc662ef54bfddd798c57c4/packages/jest-util/src/buffered_console.js#L53-L54

This method is used here:
https://github.com/facebook/jest/blob/124067e7d662364d40fc662ef54bfddd798c57c4/packages/jest-runner/src/run_test.js#L85

seems that the error comes from mock-fs, from here. Which indicates that this import to callsites is actually taken cared by the mocked fs and not by the real one.

The runTest function is imported from the test_worker which means it has the same context as the test itself (and mock-fs's).

There is a similar issue in mock-fs repo.

I'm not sure what is the wanted behavior here, but using this example should work fine.

@AriaMinaei
Copy link
Author

To add more, if mock() from mock-fs is called inside the it() block, everything works fine. The error only happens if mock() is called in the beforeEach() block.

@nknapp
Copy link

nknapp commented 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

@tschaub
Copy link

tschaub commented Apr 2, 2018

I've started on support for an overlay option for mock-fs. This should fix the lazy require issue. In the meantime, @nknapp's workaround is an alternative (calling console.log() before mocking the filesystem).

@Sujimoshi
Copy link

Sujimoshi commented Dec 13, 2018

Possible workaround

const fsMock = require('mock-fs')

let logsTemp = []
let logMock

exports.mock = (config) => {
  logMock = jest.spyOn(console, 'log').mockImplementation((...args) => {
    logsTemp.push(args)
  })
  fsMock(config)
}

exports.restore = () => {
  logMock.mockRestore()
  fsMock.restore()
  logsTemp.map(el => console.log(...el))
  logsTemp = []
}

@thymikee
Copy link
Collaborator

thymikee commented Jan 8, 2019

Does it happen with jest@beta?

@ColCh
Copy link
Contributor

ColCh commented Jun 6, 2019

happends on most recent jest@24

@github-actions
Copy link

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Feb 25, 2022
@github-actions
Copy link

This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

7 participants