Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jest-haste-map: Haste module naming collision

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)

like image 649
Vitor Camacho Avatar asked Nov 25 '19 17:11

Vitor Camacho


1 Answers

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.

like image 166
Anna Avatar answered Oct 31 '22 09:10

Anna