11

I'm getting a warning from Jest for each manual mock I have, because it finds both the .ts and .js version of it and asks me to delete one, i.e.:

jest-haste-map: duplicate manual mock found:
  Module name: feedTestData
  Duplicate Mock path: /Users/[username]/Documents/git/[projectname]/src/state/ducks/feed/__mocks__/feedTestData.ts
This warning is caused by two manual mock files with the same file name.
Jest will use the mock file found in: 
/Users/[username]/Documents/git/[projectname]/src/state/ducks/feed/__mocks__/feedTestData.ts
 Please delete one of the following two files: 
/Users/[username]/Documents/git/[projectname]/dist/state/ducks/feed/__mocks__/feedTestData.js
/Users/[username]/Documents/git/[projectname]/src/state/ducks/feed/__mocks__/feedTestData.ts

I've tried mucking around with the exclude key in my tsconfig.json, but I can't find a glob pattern that matches all __mocks__ folders.

I.e. "exclude":["**/__mocks__/*"] removes the root __mocks__ folder from my dist folder, but not for any subfolders.

For what it's worth, I'm using Expo+React Native. My Jest setup in package.json looks like this:

"jest": {
    "preset": "react-native",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "json"
    ],
    "transform": {
      "^.+\\.(js)$": "<rootDir>/node_modules/babel-jest",
      "\\.(ts|tsx)$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
    },
    "transformIgnorePatterns": [
      "node_modules/(?!(jest-)?react-native|react-navigation)"
    ],
    "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
    "testPathIgnorePatterns": [
      "\\.snap$",
      "<rootDir>/node_modules/",
      "<rootDir>/dist/"
    ],
    "cacheDirectory": ".jest/cache",
    "collectCoverageFrom": [
      "src/**/*.{ts,tsx}",
      "!**/node_modules/**",
    ],
    "setupFiles": [
      "./src/global-mock-fetch.ts",
      "./__mocks__/redux-mock-store.ts"
    ],
    "automock": false
  }
skyboyer
  • 22,209
  • 7
  • 57
  • 64
jhm
  • 4,379
  • 5
  • 33
  • 49

1 Answers1

17

is it possible to solve the problem, by maybe instructing typescript to not transpile the files in the mock directories?

I had success in an Angular + jest setup, with the following in my tsconfig:

"exclude": [
        "**/__mocks__/*"
    ]

I hope it can help you.

DauleDK
  • 3,313
  • 11
  • 55
  • 98
  • Thanks, but as mentioned in the question, I tried it :-) No dice. "I.e. "exclude":["**/__mocks__/*"] removes the root __mocks__ folder from my dist folder, but not for any subfolders." – jhm Aug 07 '18 at 06:49
  • 1
    Ok -- I looked some more at my project setup, and I can see that I eventually solved it by adding the absolute paths to solve it. So I have bookmarked the quesiton, and hope someone will come with an answer :) – DauleDK Aug 07 '18 at 08:16
  • 2
    Adding absolute paths for everything kind of sucks. – jayarjo Jun 01 '20 at 05:57
  • 3
    If '__mocks__' is still in you dist folder check if there are index.ts files with a reference to a '__mocks__' folder. I had the same problem after generating index files. The index.ts files had export * from '__mocks__' line in it. – Yoruba Jan 10 '21 at 08:48
  • @Yoruba I was getting crazy, your comment helped me a lot ! – 7hibault May 05 '22 at 09:16
  • @Yoruba, thank you, you helped me find the solution to my version of this issue. In my case my *.test.ts files weren't being excluded and were importing from the excluded __mocks__ folder. This nullified the exclude on __mocks__ and caused the error. – Andrew Jun 19 '23 at 06:14