Jest withTypeScript syntaxerror意外的标记,应为“;“

vfh0ocws  于 2023-05-27  发布在  Jest
关注(0)|答案(2)|浏览(146)

我一直在尝试设置jest与typescript一起使用,我已经尝试了一堆修复,但没有运气。我得到错误SyntaxError: Unexpected token, expected ";",我认为配置不支持typescript代码:https://i.stack.imgur.com/iGBCa.png
如果任何人有任何建议,我会非常感激!
这是我的包。json:

"jest": {
    "preset": "ts-jest",
    "testEnvironment": "node",
    "collectCoverageFrom": [
      "**/*.js",
      "**/*.ts",
      "!__tests__/util.ts",
      "!coverage/**",
      "!**/node_modules/**"
    ],
    "transform": {
      "^.+\\.tsx?$": "babel-jest"
    },
    "verbose": true,
    "automock": false,
    "clearMocks": true,
    "setupFilesAfterEnv": [
      "jest-extended"
    ],
    "testPathIgnorePatterns": [
      "/node_modules/"
    ],
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ]
  }

tsconfig.json:

{
  "compilerOptions": {
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": false,
    "isolatedModules": true,
    "jsx": "preserve",
    "lib": ["dom", "dom.iterable", "esnext"],
    "module": "esnext",
    "moduleResolution": "node",
    "noEmit": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "strict": false,
    "target": "esnext",
    "types": ["@emotion/core", "@types/jest", "jest"],
    "rootDir": ".",
    "baseUrl": ".",
    "noImplicitAny": false,
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules"]
}
rbpvctlc

rbpvctlc1#

我认为问题出在transform的配置上,当我说应该是ts-jest的时候,你却要求jest对tsx文件使用babel-jest
您甚至可以删除transform配置,因为默认情况下ts-jest预设处理tstsx

unftdfkk

unftdfkk2#

使用esbuild-jest

transform: {
    '\\.[jt]sx?$': 'esbuild-jest',
  },

相关问题