gpt4 book ai didi

javascript - Jest 使用 react + konva 和/或 react-konva 遇到了意外的 token

转载 作者:行者123 更新时间:2023-12-05 00:33:58 24 4
gpt4 key购买 nike

安装和导入 Konva 和 react-konva 后,我的 jest@enzyme 测试无法运行并出现以下错误:
● 测试套件无法运行

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".
细节:
C:\avius\fortest\node_modules\konva\lib\Core.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export { Konva } from './_CoreInternals.js';
^^^^^^

SyntaxError: Unexpected token 'export'

at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
at Object.<anonymous> (node_modules/react-konva/lib/ReactKonvaCore.js:19:13)
它是一个新创建的 create-react-app 用于测试这个错误,所以它没有什么特别之处。
我没有 babel.rc,也没有 babel.config.js 或 jest.config.js 文件。 (因为我尝试了很多设置,都不起作用。)
我可以导入和使用其他 es6 模块,如 nanoid、lodash 等,所以 konva 似乎需要一些特别的东西?卸载konva,一切正常。

最佳答案

为了让它工作,我做了以下工作:
我创建了一个 .babelrc 包含以下内容:

{
"presets": [
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-transform-modules-commonjs"
]
}
这可确保文件格式正确。
您还必须安装这些软件包。
然后我将以下内容放入我的 package.json 文件中:
"jest": {
"testEnvironment":"jsdom",
"moduleNameMapper": {
"^konva": "konva/konva"
}
}
为了更好地衡量,我重新安装了我的所有 react-konva文件(尽管这不应该有所作为)。
额外的:
如果您遇到关于未定义某些变量的问题,请确保您有 konvacanvas安装。

关于javascript - Jest 使用 react + konva 和/或 react-konva 遇到了意外的 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67919198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com