On our project, we have react-native installed in two workspaces (root folder and libraries folder for testing purposes). This throws the typical error "jest-haste-map: Haste module naming collision" when trying to test the application (root folder).
Project structure:
.(rootProject)
+-- my-package
| +-- package.json (workspace(*))
+-- package.json(workspace(*))
* has react-native as dependency
I can compile for mobile giving the following configuration:
// metro.config.js
blacklistRE: blacklist([
new RegExp(
`^${path.resolve(__dirname, 'my-package/node_modules')}/.*$`,
),
]),
For jest, I've tried the following without success:
// jest.config.js
modulePathIgnorePatterns: [
'<rootDir>/my-package/node_modules/react-native',
]
Is there any similar configuration for jest.config ?
EDIT:
The jest configuration of the root project (rootProject) (the one that doesn't work):
module.exports = {
roots: ['<rootDir>', '<rootDir>/my-package/__mocks__'],
preset: 'react-native',
snapshotSerializers: ['enzyme-to-json/serializer'],
moduleNameMapper: {
'\\.(jpg|jpeg|png|gif|svg)$':
'<rootDir>my-package/__mocks__/fileMock.js',
},
setupFiles: ['./jest.setup.js'],
testMatch: ['<rootDir>/src/**/*.spec.js'],
transformIgnorePatterns: [
'node_modules/(?!@asseco|@react-|react-|victory|rn-placeholder|redux-persist).+\\.js$',
],
coveragePathIgnorePatterns: [
'<rootDir>/node_modules',
'<rootDir>/my-package',
],
modulePathIgnorePatterns: [
'<rootDir>/my-package/node_modules/react-native',
]
coverageThreshold: {
global: {
branches: 80,
functions: 80,
lines: 80,
statements: 80,
},
},
testResultsProcessor: '<rootDir>/node_modules/jest-html-reporter',
coverageDirectory: '<rootDir>/reporters/coverage',
}
Note:
my-package is a git submodule that can be tested alone or integrated with rootProject (have his own jest configuration)
If each of your packages has a react-native dependency, you end up with two copies and this can cause problems for jest. Instead keep at as a dependency in the root project, but move it to peerDependencies
in the submodule (and a second copy to devDependencies
to be able to test it in isolation).
Are you using yalc to integrate your submodule locally? It has been a cause of the same error for me, solved by removing .yalc
folder from the root project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With