facebook / create-react-app

Set up a modern web app by running one command.
https://create-react-app.dev
MIT License
102.64k stars 26.8k forks source link

Allow manual mocks with same name #11148

Open thierryxpertsea opened 3 years ago

thierryxpertsea commented 3 years ago

Is your proposal related to a problem?

I am currently implementing a bunch of tests with manual mocks. I'd planned to name most of them index.tsx? to mock a component or hook folder exposed through an index.ts file, and I was not able to properly do it since I got this famous message:

jest-haste-map: duplicate manual mock found: index

I tried to use the "modulePathIgnorePatterns" Jest configuration to ignore local manual mocks files, but CRA does not support this option OOTB.

I then changed to use explicit names, such as AccountMenu (which leaded to do a mock call jest.mock('path/to/AccountMenu/AccountMenu'), which is not very convenient). I however reached the limit as I was expecting, since now some components share the same name for different contexts, and I get the warning message again.

Describe the solution you'd like

As a developer, I want to use the manual mock names of my choice, regardless of their uniqueness, so I can easily create standardized mocks in my application, withoutt having any kind of warning in my CI pipeline or in my local environment console.

The best solution would be just not to worry about it anymore as a developer. A default implementation of that configuration in the scripts/utils/createJestConfig.js would be great.

// Something in those lines
+ modulePathIgnorePatterns: [
+     '<rootDir>/src/.*/__mocks__',
+ ],

Describe alternatives you've considered

The whitelisting of the modulePathIgnorePatterns Jest configuration key would also be an acceptable solution, so any developer can manually adjust the configuration

FezVrasta commented 3 years ago

That'd be great, I'm hitting the same issue and there doesn't seem to be a way around it.

rbutov commented 2 years ago

_waiting for PR review ☝️ :)