Jest 给出了一个错误: “ SyntaxError: 意外令牌导出”

我在用 Jest 测试我的 React 应用。

最近,我在我的应用程序中添加了 DeckGL。我的测试失败了,出现了以下错误:

Test suite failed to run


/my_project/node_modules/deck.gl/src/react/index.js:21
export {default as DeckGL} from './deckgl';
^^^^^^


SyntaxError: Unexpected token export


at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:318:17)
at Object.<anonymous> (node_modules/deck.gl/dist/react/deckgl.js:9:14)
at Object.<anonymous> (node_modules/deck.gl/dist/react/index.js:7:15)

这看起来像是 Jest 在运行测试之前转换节点模块时出现的问题。

这是我的. babelrc:

{
"presets": ["react", "es2015", "stage-1"]
}

这是我的玩笑设置:

"jest": {
"testURL": "http://localhost",
"setupFiles": [
"./test/jestsetup.js"
],
"snapshotSerializers": [
"<rootDir>/node_modules/enzyme-to-json/serializer"
],
"moduleDirectories": [
"node_modules",
"/src"
],
"moduleNameMapper": {
"\\.(css|scss)$": "<rootDir>/test/EmptyModule.js"
}
},

我似乎有正确的事情需要转换 export {default as DeckGL }。那么有什么想法出错了吗?

128022 次浏览

默认情况下,Jest 不会在 node_modules目录中编译文件。

转换忽略模式 [数组]

默认值: [“/node _ module/”]

与所有源匹配的 regexp 模式字符串数组 文件路径。如果测试路径匹配 模式,则不会进行转换

DeckGL 似乎在 ES6中,为了使 jest 能够读取它,您还需要编译它。
为此,只需在 transformignorePatterns中为 DeckGL 添加一个异常

"transformIgnorePatterns": ["/node_modules/(?!deck\.gl)"]

Https://facebook.github.io/jest/docs/en/configuration.html#transformignorepatterns-array-string

这意味着,文件不会通过 TypeScript 编译器进行转换,例如,因为它是一个具有 TS 语法的 JS 文件,或者它作为未编译的源文件发布到 npm。你可以这么做。

调整你的 transformIgnorePatterns允许列表:

{
"jest": {
"transformIgnorePatterns": [
"node_modules/(?!@ngrx|(?!deck.gl)|ng-dynamic)"
]
}
}

默认情况下,Jest 不会转换 node _ module,因为它们应该是有效的 JavaScript 文件。然而,恰好库作者假设您将编译他们的源代码。所以你必须把这个明确地告诉开玩笑。上面的代码片段意味着@ngrx、 deck 和 ng-Dynamic 将被转换,即使它们是 node _ module。

这是因为 Node.js 不能处理 ES6模块。

因此,您应该将您的模块转换为 CommonJS。

巴别塔7

安装 npm install --save-dev @babel/plugin-transform-modules-commonjs

为了只用于测试用例,添加到.babelrc, Jest 自动给出 NODE _ ENV = test 全局变量。

"env": {
"test": {
"plugins": ["@babel/plugin-transform-modules-commonjs"]
}
}

巴别塔

npm install --save-dev babel-plugin-transform-es2015-modules-commonjs

到 Babelrc

"env": {
"test": {
"plugins": ["transform-es2015-modules-commonjs"]
}
}

如果您使用的是“ create-response-app”,它将不允许您通过 包裹 Json中的 Jest 属性指定 “转换忽略模式”

根据这个 https://github.com/facebook/create-react-app/issues/2537#issuecomment-390341713

您可以在 包裹 Json中使用下面的 CLI 来覆盖它,它可以工作:

"scripts": {
"test": "react-scripts test --transformIgnorePatterns \"node_modules/(?!your-module-name)/\"",
},

我有个单核细胞增多症的问题。根 node_modules中的一个包破坏了我的测试。我通过将本地 .babelrc文件更改为 babel.config.js来修复。说明: https://github.com/facebook/jest/issues/6053#issuecomment-383632515

这个代码对我很有用 巴贝尔

{
"presets": [
["env", {
"modules": "commonjs", // <- Check and see if you have this line
"targets": {
"browsers": ["> 1%", "last 2 versions", "not ie <= 8"]
}
}],
"stage-2"
],
"plugins": ["transform-vue-jsx", "transform-runtime"],
"env": {
"test": {
"presets": ["env", "stage-2"],
"plugins": ["transform-vue-jsx", "transform-es2015-modules-commonjs", "dynamic-import-node"]
}
}
}

Jest 理解 commonJs,因此在使用之前需要 babel 来转换代码。Jest 在运行代码时也使用缓存。因此,在运行 jest 之前,一定要运行 jest --clearCache

测试环境:
Node v8.13.0
Babel v6 +
Jest v27

我在从 vis-data. js 库导入 dataSet 时也犯了同样的错误

import { DataSet } from 'vis-data/esnext';

所以我把 /esnext从路径中移除,现在它可以工作了:

import { DataSet } from 'vis-data';

这是围绕在 这一页 # 1的工作,为我修复了它,虽然在该页面上的工作方法 # 2在上述答案中提到,所以他们也可能是有效的。

”在 moduleNameMapper配置中指定相应包的 common js 版本的条目

jest.config.js

    moduleNameMapper: {
"^uuid$": require.resolve("uuid"),
"^jsonpath-plus$": require.resolve("jsonpath-plus")
...

在我的例子中,我在 package.json 文件中使用这个配置:

"jest": {
"transformIgnorePatterns": [
"!node_modules/"
]
}

我正在使用 monorepo (它包含前端和后端的多个包)。

我正在测试的包从使用依赖项 uuid的另一个包导入一个文件。

所有的文件都是类型(而不是 Javascript)。

我正在测试的包有一个仅用于测试的 tsconfig 文件,称为 tsconfig.test.json。它具有 commonjsallowJs的性质。添加 allowJs解决了导入 uuid时的问题,我不知道为什么。

{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": [
"jest",
"node"
],
// Necessary to import dependency uuid using CommonJS
"allowJs": true
},
"include": [
"jest.config.ts",
"**/*.test.ts",
"**/*.d.ts"
]
}

我当时正在升级一个使用读取 .babelrc配置的 babel 版本的项目,当我升级到一个更新的版本时,我读到:

Https://babeljs.io/docs/en/configuration#whats-your-use-case

你的用例是什么?

你在用止痛药吗?

要编译 node _ module 吗?

Json 是为您准备的!

最重要的是:

{
"jest": {
"transformIgnorePatterns": [
"node_modules/(?!(module))"
]
}
}

我也把 .babelrc改成了 babel.config.json