【发布时间】:2021-10-16 00:12:11
【问题描述】:
我在我的 Vue 应用程序中添加了对 tiptap-vuetify 的依赖,这反过来又添加了对其他 19 个模块的传递依赖。添加后,我运行测试并收到以下错误
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
SyntaxError: Unexpected token 'export'
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 node_modules/tiptap-vuetify/dist/bundle-umd.js:1:112
at Object.<anonymous> (node_modules/tiptap-vuetify/dist/bundle-umd.js:1:2)
因此,tiptap-vuetify 似乎包含 Jest 需要转译的代码。根据Jest docs 将以下内容添加到jest.config.js 应该可以解决问题,如果未编译的代码位于tiptap 或tiptap-vuetify 中
transformIgnorePatterns: [
"node_modules/(?!(tiptap-vuetify|tiptap)/)"
]
但这并没有什么不同。我添加了以下内容:
transformIgnorePatterns: ['/node_modules/tiptap.*']
并且问题不再发生,但我想我可能正在转换我的整个依赖关系树,因为现在运行测试需要更长的时间。有没有更好的方法来解决这个问题?
【问题讨论】:
标签: javascript vue.js jestjs babel-jest