create-react-app icon indicating copy to clipboard operation
create-react-app copied to clipboard

Allow manual mocks with same name

Open thierryxpertsea opened this issue 4 years ago • 3 comments

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

thierryxpertsea avatar Jun 25 '21 21:06 thierryxpertsea

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

FezVrasta avatar Sep 13 '21 16:09 FezVrasta

_waiting for PR review ☝️ :)

rbutov avatar Aug 17 '22 22:08 rbutov

Thanks @rbutov, I ended up adding modulePathIgnorePatterns: ['<rootDir>/src/.*/__mocks__'], to my Jest config

rj-au avatar Jun 13 '25 04:06 rj-au