如何避免使用Typescript转换Jest __mocks__

时间:2018-03-20 15:43:43

标签: typescript react-native jestjs expo

我收到Jest的警告,因为它找到了.ts和.js版本,并要求我删除一个,即:

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

我已经尝试使用exclude中的tsconfig.json密钥,但我无法找到与所有__mocks__个文件夹匹配的glob模式。

即。 "exclude":["**/__mocks__/*"]从我的__mocks__文件夹中删除根dist文件夹,但不删除任何子文件夹。

为了它的价值,我使用的是Expo + React Native。我在package.json中的Jest设置如下所示:

"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
  }

1 个答案:

答案 0 :(得分:2)

是否可以通过指示打字稿不要在模拟目录中转储文件来解决此问题?

我在Angular +开玩笑的设置中取得了成功,并在tsconfig中添加了以下内容:

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

希望它能对您有所帮助。