Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid Typescript transpiling Jest __mocks__

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
  }
like image 773
jhm Avatar asked Mar 20 '18 15:03

jhm


1 Answers

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.

like image 145
DauleDK Avatar answered Nov 16 '22 10:11

DauleDK