How can I have all my tests and mocks inside a folder "tests" next to my "app" source code?
#2726 opened on Jan 28, 2017
Description
I currently have all my app source code under app/ and all my tests source code under tests/. These folders follow the same folder structure. Everything is working fine with the following Jest configuration:
{
"collectCoverageFrom": [
"app/**/*.{ts,tsx}"
],
"coverageDirectory": "build/coverage/",
"coverageReporters": [
"html",
"text-summary"
],
"moduleFileExtensions": [
"js",
"ts",
"tsx"
],
"moduleNameMapper": {
"^.+\\.css$": "identity-obj-proxy"
},
"modulePaths": [
"<rootDir>/app/"
],
"snapshotSerializers": [
"enzyme-to-json/serializer"
],
"transform": {
"^.+\\.(ts|tsx)$": "./utils/jest-typescript-transformer.js",
"^.+\\.(gif|jpg|png)$": "./utils/jest-image-size-transformer.js"
},
"testPathDirs": [
"<rootDir>/app/",
"<rootDir>/tests/"
],
"testRegex": "tests/.+\\.spec\\.(ts|tsx)$"
}
Now I need to mock some modules and the documentation states:
Manual mocks are defined by writing a module in a
__mocks__/subdirectory immediately adjacent to the module.
This means that the __mocks__ needs to be inside the app folder but I'd like to keep everything test related inside the tests folder. But there's the thing, if I have duplicate mocks on both app and tests I get this:
jest-haste-map: duplicate manual mock found:
Module name: DialpadDigits
Duplicate Mock path: C:\SampleApp\tests\constants\dialer\__mocks__\DialpadDigits.ts
This warning is caused by two manual mock files with the same file name.
Jest will use the mock file found in:
C:\SampleApp\tests\constants\dialer\__mocks__\DialpadDigits.ts
Please delete one of the following two files:
C:\SampleApp\app\constants\dialer\__mocks__\DialpadDigits.ts
C:\SampleApp\tests\constants\dialer\__mocks__\DialpadDigits.ts
But the only file actually being used is the one under app... How do I know this? Say I have the same mock on both folders and this mock fails my tests and if I don't use the mock, the tests pass. If I remove the mocked module from app, the test passes. Which means the mock from tests is not being used, despite that Jest warning above.
To sum up, how can I have my mocks in the tests folder?