【问题标题】:Jest test fails at tsx syntaxJest 测试在 tsx 语法上失败
【发布时间】:2020-09-05 06:14:46
【问题描述】:

我目前正在对我的项目实施开玩笑测试。该项目由 ts + tsx 文件组成。以下是我的jest.config.js 文件。

module.exports = {
  preset: "ts-jest",
  testEnvironment: "jsdom",
  cacheDirectory: ".jest-cache",
  coverageDirectory: ".jest-coverage",
  coverageReporters: ["html", "text"],
  testMatch: ["**/target/**/components/**/__tests__/**/*.[jt]s?(x)"],
  transformIgnorePatterns: [
    "node_modules/(?!@ngrx|(?!deck.gl)|ng-dynamic)",
    "/app-extensions/"
  ],
  transform: {
    "^.+\\.jsx?$": "babel-jest",
    "^.+\\.ts?$": "ts-jest",
    '^.+\\.tsx?$': "babel-jest"
  },
  moduleNameMapper: {
    "\\.(css|jpg|png|svg)$": "<rootDir>/empty-module.js"
  },
  coverageThreshold: {
    global: {
      branches: 100,
      functions: 100,
      lines: 100,
      statements: 100
    }
  },
};

但是当我构建应用程序时,会抛出以下错误并失败。我在这里做错了什么?我尝试使用 ts-jest 和 babel-jest 转换 tsx 文件,但都因此错误而失败。

【问题讨论】:

    标签: reactjs typescript jestjs ts-jest


    【解决方案1】:

    .tsx 文件是用 Babel 而不是 TypeScript 处理的,Babel 不知道: ReactElement 类型部分。

    ^.+\\.ts?$ 正则表达式不正确,因为它匹配 .t 和 .ts 文件,但不匹配 .tsx。

    应该是:

      transform: {
        "^.+\\.jsx?$": "babel-jest",
        '^.+\\.tsx?$': "ts-jest"
      },
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-01
      • 1970-01-01
      • 2021-04-04
      • 1970-01-01
      • 2019-03-19
      • 2022-10-18
      • 2018-12-17
      相关资源
      最近更新 更多