How to handle duplicated manual mock found in the project

Viewed 9110

The project has both client app and server site code and once the project is built locally it will put all the built assets both front-end and back-end stuff into build directory and it contains all statics assets client needs to use (all test cases are also included there).

When running the subsequent tests Jest will raise a warning message like below due to duplicated manual mock are existing in multiple directories.

jest-haste-map: duplicate manual mock found:
  Module name: fileMock
  Duplicate Mock path: /Users/x/x/x/x/src/resources/webapp/static/__mocks__/fileMock.js
This warning is caused by two manual mock files with the same file name.
Jest will use the mock file found in:
/Users/x/x/x/x/src/resources/webapp/static/__mocks__/fileMock.js
 Please delete one of the following two files:
 /Users/x/x/x/x/build/classes/webapp/static/__mocks__/fileMock.js
/Users/x/x/x/x/src/resources/webapp/static/__mocks__/fileMock.js
2 Answers

Put "<rootDir>/build" inside modulePathIgnorePatterns, the rootDir is not a placeholder but a special token Jest can recognise, https://jestjs.io/docs/en/configuration#rootdir-string

Note that the <rootDir> is NOT a placeholder but a special token Jest uses to identifier the root of your project directory.

    "jest": {
        "modulePathIgnorePatterns": [
            "<rootDir>/build"
        ]
    }

Add the build folder into modulePathIgnorePatterns inside your package.json file.

Related