Jest 无法解析文件 - 无法在模块外部使用 import 语句

and*_*dko 9 typescript jestjs babeljs ts-jest

我想将 Jest 与 TypeScript 和 React 一起使用,但测试套件因以下错误而失败:

\n
Jest encountered an unexpected token\n\n    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.\n\n    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.\n\n    By default "node_modules" folder is ignored by transformers.\n\n    Here's what you can do:\n     \xe2\x80\xa2 If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.\n     \xe2\x80\xa2 If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript\n     \xe2\x80\xa2 To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.\n     \xe2\x80\xa2 If you need a custom transformation specify a "transform" option in your config.\n     \xe2\x80\xa2 If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.\n\n    You'll find more details and examples of these config options in the docs:\n    https://jestjs.io/docs/configuration\n    For information about custom transformations, see:\n    https://jestjs.io/docs/code-transformation\n\n    Details:\n\n    somepath/node_modules/mana-syringe/src/inversify/index.ts:1\n    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import type { interfaces } from 'inversify';\n                                                                                      ^^^^^^\n\n    SyntaxError: Cannot use import statement outside a module\n\n      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)\n      at Object.<anonymous> (node_modules/mana-syringe/dist/index.js:5:17)\n
Run Code Online (Sandbox Code Playgroud)\n

设置如下:
\n babel.config.js

\n
module.exports = {\n  presets: ['@babel/preset-env', ['@babel/preset-react', { runtime: 'automatic' }], '@babel/preset-typescript'],\n  plugins: [['module-resolver', { root: ['./src'] }]]\n};\n
Run Code Online (Sandbox Code Playgroud)\n

package.json 中的笑话部分

\n
"jest": {\n    "roots": ["<rootDir>/src"],\n    "preset": "ts-jest",\n    "collectCoverage": true,\n    "coverageDirectory": "coverage",\n    "moduleFileExtensions": [\n      "js",\n      "jsx",\n      "ts",\n      "tsx",\n      "json",\n      "node"\n    ],\n    "moduleDirectories": [\n      "node_modules",\n      "src"\n    ],\n    "moduleNameMapper": {\n      "\\\\.(css|less|scss)$": "identity-obj-proxy"\n    },\n    "testEnvironment": "jsdom",\n    "transform": {\n      "^.+\\\\.(ts|tsx)?$": "ts-jest",\n      "^.+\\\\.(js|jsx)$": "babel-jest"\n    },\n    "testRegex": "(/__tests__/.*|(\\\\.|/)(test|spec))\\\\.(jsx?|tsx?)$",\n    "testPathIgnorePatterns": [\n      "/node_modules/"\n    ],\n    "verbose": true,\n    "setupFilesAfterEnv": [\n      "<rootDir>/setupEnzyme.ts"\n    ],\n    "globals": {\n      "ts-jest": {\n        "babelConfig": true\n      }\n    }\n  }\n
Run Code Online (Sandbox Code Playgroud)\n

tsconfig.json

\n
{\n  "compilerOptions": {\n    "strict": true,\n    "outDir": "./lib/tsc",\n    "declarationDir": "./lib/tsc",\n    "module": "ESNext",\n    "moduleResolution": "Node",\n    "resolveJsonModule": true,\n    "isolatedModules": true,\n    "target": "es5",\n    "lib": ["DOM", "DOM.Iterable", "ESNext"],\n    "allowJs": true,\n    "jsx": "react",\n    "declaration": true,\n    "noImplicitAny": true,\n    "skipLibCheck": true,\n    "esModuleInterop": true,\n    "allowSyntheticDefaultImports": true\n  },\n  "include": [\n    "src/**/*",\n    "types",\n    "@types/jest"\n  ],\n  "exclude": [\n    "node_modules/*"\n  ]\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我在这里做错了什么?

\n