使用ts-jest和create-react-app

时间:2018-02-14 15:52:33

标签: reactjs typescript jest create-react-app react-scripts

使用create-react-appreact-scripts-ts一起使用TypeScript时,使用--coverage标记运行测试会导致覆盖率报告不正确。是否有任何方法可以整合ts-jest以使覆盖率报告准确无误?

以下是我在package.json中的jest配置:

"jest": {
    "transform": {
      "^.+\\.tsx?$": "ts-jest"
    },
    "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ],
    "mapCoverage": true,
    "coverageThreshold": {
      "global": {
        "branches": 100,
        "functions": 100,
        "lines": 100,
        "statements": 100
      }
    }
  }
}

编辑:这是我收到的错误消息:

Out of the box, Create React App only supports overriding these Jest options:

  • collectCoverageFrom
  • coverageReporters
  • coverageThreshold
  • snapshotSerializers.

These options in your package.json Jest configuration are not currently supported by Create React App:

  • transform
  • testRegex
  • moduleFileExtensions
  • mapCoverage

If you wish to override other Jest options, you need to eject from the default setup. You can do so by running npm run eject but remember that this is a one-way operation. You may also file an issue with Create React App to discuss supporting more options out of the box.

1 个答案:

答案 0 :(得分:1)

您是否有理由使用react-scripts-ts / ts-jest而不是常规的现成Create React App?一段时间以来,它一直在本地支持TypeScript。我建议这样做,因为与CRA的默认值作斗争通常是一个痛点。

相关问题