Skip to content

Allow node_modules to be mocked as described in the Jest documentation #8257

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

KennethSundqvist
Copy link

@KennethSundqvist KennethSundqvist commented Dec 30, 2019

This PR fixes #7539.

The Jest documentation says:

If the module you are mocking is a Node module (e.g.: lodash), the mock should be placed in the __mocks__ directory adjacent to node_modules (unless you configured roots to point to a folder other than the project root) and will be automatically mocked.

We have to check if the directory exists before we add it because the watcher used by Jest (jest-haste-map > './lib/FSEventsWatcher.js' > walker) will throw an error if '<rootDir>/__mocks__' doesn't exist.

@facebook-github-bot
Copy link

Hi KennethSundqvist! Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file.In order for us to review and merge your code, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@ianschmitz ianschmitz added this to the 3.4 milestone Jan 30, 2020
@iansu iansu modified the milestones: 3.4, 3.5 Feb 14, 2020
@gitonthescene
Copy link

Hi there! Is the only thing left here for this change to be reviewed?

@slamflipstrom
Copy link

If this fix is ready, my team and I would be very grateful to have this merged. This has caused us some pain and effort working through alternative mocking solutions while module mocks have been broken.

Thank you for all of the great work and we hope to see this fix released soon!

@lewisloofis
Copy link

Hello, isn’t this PR ready to be merged? I have been waiting for this fix in CRA for cleaner jest config in my project

@StarryFire
Copy link

I cannot automatically mock node_modules in my react native project either! Waiting for this PR!

@elliotchaim
Copy link

Can this be merged please?

@borrascador
Copy link

borrascador commented Nov 11, 2020

@bugzpodder @ianschmitz Looking at the PR that caused this bug, #7480 and the related issue #7458. The time from raising the issue (August 1st 2019) to merging the PR (August 6th 2019) was a mere five days. From the time the PR got open (August 5th 2019) to closing was only one day! Reading through both threads, it looks a lot like there was a slowdown for ONE person, and this was immediately rubber-stamped and merged without questioning the breaking changes to documented jest usage.

Compare this to #7539. Opened just ten days later! Cosigned by nearly a dozen people. Reverting the change from #7480 got blown off, so here we are with a fix, and it's been languishing for nearly a year. Over a year, if you count from the moment the issue was opened.

Nobody had to line up and say please to get the breaking change in... and a year later there's still no documentation that jest's defaults have been radically changed.

Furthermore, I'd argue that optimizing a CPU bottleneck at the expense of developer time spent chasing undocumented bugs is a pretty alarming trade-off.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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