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.

node_modules Mocks are not picked up by jest with latest react-scripts

See original GitHub issue

Describe the bug

When upgrading from react-scripts@3.0.1 to latest the mocks are no longer picked up from /__mocks__ directory

Did you try recovering your dependencies?

YES

Which terms did you search for in User Guide?

jest manual mocks broken

Environment

Environment Info:

System: OS: macOS 10.14.5 CPU: (8) x64 Intel® Core™ i7-7920HQ CPU @ 3.10GHz Binaries: Node: 12.8.0 - /usr/local/bin/node Yarn: 1.17.3 - ~/.yarn/bin/yarn npm: 6.10.2 - /usr/local/bin/npm Browsers: Chrome: 76.0.3809.100 Firefox: 68.0.1 Safari: 12.1.1 npmPackages: react: ^16.9.0 => 16.9.0 react-dom: ^16.9.0 => 16.9.0 react-scripts: 3.1.1 => 3.1.1 npmGlobalPackages: create-react-app: 2.1.3

Steps to reproduce

(Write your steps here:)

  1. create a manual mock of any node_modules package
  2. put it to mocks directory
  3. run your test

Expected behavior

Mocks are picked up by jest

Actual behavior

Mocks are not picked up

Reproducible demo

(Paste the link to an example project and exact instructions to reproduce the issue.)

  1. git clone https://github.com/tomitrescak/react-boilerplate -b Demo
  2. cd react-boilerplate
  3. yarn
  4. yarn test
  5. p -> ‘button’
  6. error: Trans not found (not being picked up by mocks)

To see that is works with previous react-scripts do

  1. yarn add react-scripts@3.0.1
  2. yarn test
  3. You will recieve a snapshot erro which is fine

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:36
  • Comments:27 (5 by maintainers)

github_iconTop GitHub Comments

30reactions
brendanmc6commented, Mar 3, 2020

For those whose test’s are completely broken by this: Moving __mocks__ under ./src solved the problem for me.

23reactions
ultimagrievercommented, Sep 14, 2019

@brendanmc6 the issue is this is an anti-pattern.

The Jest documentation says that any external module manual mocks must live in the same level as node_modules, see https://jestjs.io/docs/en/manual-mocks#mocking-node-modules

Read more comments on GitHub >

github_iconTop Results From Across the Web

jestjs - Create React App doesn't properly mock modules from ...
The problem is that CRA's default Jest setup automatically resets the mocks, which removes the mockResolvedValue you set.
Read more >
Create react app typescript: testing with jest and enzyme
When running npm run test a new snapshot will be created and there will be a new folder generated __snapshots__ with it. running...
Read more >
How I resolved issues while setting up Jest and Enzyme in a ...
The main reason for writing this blog is that I was facing a lot of errors while setting up the testing environment in...
Read more >
Troubleshooting - Jest
The transform script was changed or Babel was updated and the changes aren't being recognized by Jest? Retry with --no-cache . Jest caches ......
Read more >
Configuring package.json · Jest
When using the --config option, the JSON file must not contain a "jest" key: ... coverage information will be collected for it even...
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