10

当使用create-react-appwithreact-scripts-ts来使用 TypeScript 时,使用--coverage标志运行测试会导致不正确的覆盖率报告。有什么方法可以集成 ts-jest 以使覆盖率报告准确吗?

以下是我的笑话配置package.json

"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.
4

1 回答 1

1

您是否有任何理由使用react-scripts-ts/ts-jest而不是常规的开箱即用的 Create React App?它已经原生支持 TypeScript 有一段时间了。我建议这样做,因为与 CRA 的违约作斗争通常是一个痛点。

于 2020-09-29T14:21:30.730 回答