将ts-jest与create-react-app一起使用

hol*_*ltc 5 typescript reactjs jest react-scripts create-react-app

使用create-react-appwith react-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
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

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

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.
Run Code Online (Sandbox Code Playgroud)

tor*_*kel 2

您使用react-scripts-ts/ts-jest而不是常规的开箱即用的 Create React App 有什么原因吗?它原生支持 TypeScript 一段时间了。我建议这样做,因为对抗 CRA 的默认设置通常是一个痛点。