Jest在使用React TypeScript时遇到了意外的标记。

14
我正在构建一个可重用的React组件,而不使用react-app,并且我对Jest非常陌生。 我一直收到这个消息。 我已经尝试了Stackoverflow上的几个解决方案,但目前还卡住了:
●测试套件运行失败
Jest encountered an unexpected token

This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

Here's what you can do:
 • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
 • If you need a custom transformation specify a "transform" option in your config.
 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html

Details:

C:\Users\User\Documents\accessible-date-picker\src\__tests__\DatePicker.spec.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import { DatePicker } from '../containers/DatePicker';
                                                                                         ^^^^^^

SyntaxError: Cannot use import statement outside a module

  at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)

我有以下配置,但我无法弄清为什么无法解决问题:
//jest.config.js
module.exports = {
  roots: ["<rootDir>/src"],
  testMatch: [
    "**/__tests__/**/*.+(ts|tsx|js)",
    "**/?(*.)+(spec|test).+(ts|tsx|js)",
  ],
  transform: {
    "^.+\\.(ts|tsx)$": "ts-jest",
  },
  coveragePathIgnorePatterns: [
    "/node_modules/"
  ],
  moduleNameMapper: {
    "\\.(css|less)$": "identity-obj-proxy",
  }
};

以下是我的 tsconfigurations:

{
    "compilerOptions": {
        "lib": [
            "dom",
            "dom.iterable",
            "esnext"
        ],
        "allowJs": true,
        "allowSyntheticDefaultImports": true,
        "skipLibCheck": true,
        "esModuleInterop": true,
        "strict": true,
        "forceConsistentCasingInFileNames": true,
        "moduleResolution": "node",
        "resolveJsonModule": true,
        "isolatedModules": true,
        "noEmit": true,
        "jsx": "react"
    },
    "include": [
        "src"
    ]
}

以下是我的开发依赖项:

 "devDependencies": {
    "@babel/core": "^7.12.7",
    "@babel/plugin-transform-runtime": "^7.12.1",
    "@babel/preset-env": "^7.12.7",
    "@babel/preset-react": "^7.12.7",
    "@babel/preset-typescript": "^7.12.7",
    "@babel/runtime": "^7.12.5",
    "@teamsupercell/typings-for-css-modules-loader": "^2.4.0",
    "@types/fork-ts-checker-webpack-plugin": "^0.4.5",
    "@types/jest": "^26.0.15",
    "@types/react": "^17.0.0",
    "@types/react-dom": "^17.0.0",
    "@types/webpack": "^4.41.25",
    "@types/webpack-dev-server": "^3.11.1",
    "@typescript-eslint/eslint-plugin": "^4.8.1",
    "@typescript-eslint/parser": "^4.8.1",
    "babel-loader": "^8.2.1",
    "css-loader": "^5.0.1",
    "eslint": "^7.14.0",
    "eslint-plugin-react": "^7.21.5",
    "eslint-plugin-react-hooks": "^4.2.0",
    "fork-ts-checker-webpack-plugin": "^6.0.3",
    "jest": "^26.6.3",
    "style-loader": "^2.0.0",
    "ts-jest": "^26.4.4",
    "ts-node": "^9.0.0",
    "typescript": "^4.1.2",
    "webpack": "^5.6.0",
    "webpack-cli": "^4.2.0",
    "webpack-dev-server": "^3.11.0"
  }
}

非常感谢您提供的任何帮助!

你能分享一下你的ts配置文件吗? - tmhao2005
谢谢您指出这一点。我已经在我的帖子中添加了它。 - rockon_d
1个回答

17

看起来你的测试文件是一个 js 文件(src\__tests__\DatePicker.spec.js),而不是 ts?x 文件,这意味着这个模式永远不会满足 "^.+\\.(ts|tsx)$": "ts-jest"

然而,你可能知道 tsc 也可以转译你的 js 代码,只要你设置了 allowJs: true,就像你已经做的那样。所以我认为你的问题将在你调整模式以包括上述 jsx 文件时得到解决:

{
  transform: {
    "^.+\\.(t|j)sx?$": "ts-jest",
  }
}

1
谢谢!这刚刚解决了很多的挫败感 :D - rockon_d
1
这个有效,谢谢! 在我的情况下,我收到了相同的错误,但是来自文件crypto-random-string\index.js。由于这是在node_modules中,除了上面的答案之外,我还必须将以下片段添加到我的jest.config.js中: transformIgnorePatterns: ["<rootDir>/node_modules/(?!crypto-random-string/)"] - jrsmolley

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接