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]: Mocking the implementation of path.resolve causes internal Jest errors #14182

Closed
awarrier99 opened this issue Jun 4, 2023 · 8 comments

Comments

@awarrier99
Copy link

Version

29.5.0

Steps to reproduce

  1. Clone my repo at https:/awarrier99/jest-path-resolve-mock-bug
  2. Run yarn install and then yarn test example-valid.test.js, which should pass
  3. Run yarn test example-invalid.test.js, which should report the error:
Test suite failed to run

    Cannot find module 'jest-util'
    Require stack:
    - /<...>/jest-path-resolve-mock-bug/node_modules/jest-runner/build/index.js
    - /<...>/jest-path-resolve-mock-bug/node_modules/jest-util/build/requireOrImportModule.js
    - /<...>/jest-path-resolve-mock-bug/node_modules/jest-util/build/index.js
    - /<...>/jest-path-resolve-mock-bug/node_modules/jest-config/build/getCacheDirectory.js
    - /<...>/jest-path-resolve-mock-bug/node_modules/jest-config/build/Defaults.js
    - /<...>/jest-path-resolve-mock-bug/node_modules/jest-config/build/normalize.js
    - /<...>/jest-path-resolve-mock-bug/node_modules/jest-config/build/index.js
    - /<...>/jest-path-resolve-mock-bug/node_modules/jest-cli/build/init/index.js
    - /<...>/jest-path-resolve-mock-bug/node_modules/jest-cli/build/run.js
    - /<...>/jest-path-resolve-mock-bug/node_modules/jest-cli/build/index.js
    - /<...>/jest-path-resolve-mock-bug/node_modules/jest-cli/bin/jest.js
    - /<...>/jest-path-resolve-mock-bug/node_modules/jest/bin/jest.js

      at _jestUtil (node_modules/jest-runner/build/index.js:41:16)
  1. I wasn't able to reproduce it in this case where yarn test (with no passed filenames) would also fail, but in my actual project where I'm experiencing this issue, the tests would fail no matter what

Expected behavior

I expect to be able to mock path.resolve without having to worry about breaking the internals of Jest

Actual behavior

I received an error stating either Cannot find module 'jest-util' or Cannot find module 'jest-worker' depending how I ran the tests in my project

Additional context

I did attempt to search for this issue on Google, the Jest GitHub issue tracker, and the Jest documentation. I saw a few answers suggesting that this may be due to an incomplete installation of jest. However, as you can see, the tests run fine in the same environment by simply removing the permanent mock of path.resolve. The test cases here might seem a bit contrived since they're doing the same thing, but in my actual project it's convenient to be able to mock path.resolve just once in a describe as I have several distinct test cases of the same function which rely on that mock.

I am able to solve this issue by using a different approach (such as the beforeEach + mockReturnValueOnce approach used in the example valid test file), but in general I think it would make sense that a jest mock should not break its own ability to run tests. I have also found that mocking path.resolve can cause requires or jest.requireActual calls to break

Environment

System:
    OS: macOS 13.3.1
    CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
  Binaries:
    Node: 18.12.1 - ~/.nvm/versions/node/v18.12.1/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.12.1/bin/yarn
    npm: 8.19.4 - ~/.nvm/versions/node/v18.12.1/bin/npm
  npmPackages:
    jest: ^29.5.0 => 29.5.0
@github-actions
Copy link

github-actions bot commented Jul 4, 2023

This issue is stale because it has been open 30 days 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 Jul 4, 2023
@awarrier99
Copy link
Author

Just commenting so the issue doesn't close - this error does still occur with the current latest version

@github-actions github-actions bot removed the Stale label Aug 1, 2023
@github-actions
Copy link

This issue is stale because it has been open 30 days 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 Aug 31, 2023
@github-actions
Copy link

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

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 30, 2023
@github-actions
Copy link

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

@SimenB
Copy link
Member

SimenB commented Oct 2, 2023

This is nodejs/node#31852 FWIW

@awarrier99
Copy link
Author

@SimenB Ah I see, thanks for the context!

Copy link

github-actions bot commented Nov 2, 2023

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 Nov 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants