我在react应用程序中使用了一个作为.tgz文件的npm包。但是,当我尝试使用导入该包的jest测试react组件时,我得到了以下错误。我是否需要配置一些东西来测试这一点?
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:
• 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:
D:applicationnode_modules@clientiorappidbuildpackageindex.ts:14
export * from './rappid';
^^^^^^
SyntaxError: Unexpected token 'export'
> 4 | import * as joint from '@clientio/rappid/build/package';
| ^
at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
at Object.<anonymous> (src/../hmy-component.js:4:1)
我的包。Json如下所示:
{
...
"dependencies": {
...
"@clientio/rappid": "file:vendor/rappid-3.2.0.tgz"
...
}
组件如下图所示。
...
import * as joint from '@clientio/rappid/build/package';
...
function MyComponent(props) {
...
}
给出答案有点晚了,但也许有人会觉得有用。
Jest在上面的输出中建议的解决方案是正确的。您应该将transformIgnorePatterns
添加到Jest配置中。
如果应用使用了'create-react-app',可以把它作为一个选项添加到package.json的npm脚本中。我认为有些Jest配置选项在"create-react-app"中是不可用的。如果你不使用'create-react-app',你可以直接把它添加到Jest配置中。
"test": "react-scripts test --transformIgnorePatterns "node_modules/(?!<package_name>)/""
一个类似的堆栈溢出问题可以在这里找到参考。