javascript - 使用 Jest 运行测试时转换依赖项
问题描述
我在我的 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 文档jest.config.js
,如果未转译的代码在tiptap 或tiptap-vuetify 中,则添加以下内容应该可以解决问题
transformIgnorePatterns: [
"node_modules/(?!(tiptap-vuetify|tiptap)/)"
]
但这没有任何区别。我添加了以下内容:
transformIgnorePatterns: ['/node_modules/tiptap.*']
并且问题不再发生,但我想我可能正在编译我的整个依赖关系树,因为现在运行测试需要更长的时间。有没有更好的方法来解决这个问题?
解决方案
tiptap-vuetify
不是导致错误的实际模块,尽管有误导性的堆栈跟踪。
错误很可能来自,如Jest 错误vuetify
的输出所示:Details
Jest encountered an unexpected token
//...
Details:
/Users/tony/src/tmp/vue2-vuetify-test/node_modules/vuetify/lib/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export * from './components';
^^^^^^
SyntaxError: Unexpected token 'export'
11 | <script>
12 | // import the component and the necessary extensions
> 13 | import { TiptapVuetify, Heading, Bold, Italic, Strike, Underline, Code, Paragraph, BulletList, OrderedList, ListItem, Link, Blockquote, HardBreak, HorizontalRule, History } from 'tiptap-vuetify'
| ^
14 |
15 | export default {
16 | // specify TiptapVuetify component in "components"
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
at node_modules/tiptap-vuetify/dist/bundle-umd.js:1:201
at Object.<anonymous> (node_modules/tiptap-vuetify/dist/bundle-umd.js:1:429)
at Object.<anonymous> (src/components/MyTipTap.vue:13:1)
确保您的transformIgnorePatterns
排除vuetify
项以确保它被转译:
// jest.config.js
module.exports = {
transformIgnorePatterns: ['/node_modules/(?!vuetify)'],
}
推荐阅读
- python - 为什么它说 UnboundLocalError: local variable 'max_range' referenced before assignment 当我分配它时
- react-native - 无法访问 Android 设备中的位置(访问被拒绝,错误代码 3,没有可用的位置提供程序)
- angular - 使用cordova在模拟器上获得白屏
- reactjs - 如何在 React Native 中实现类似的功能?
- python - 如何将窗口大小调整为 selenium 上的 deviceEmulator 尺寸
- mysql - 将sql查询组合在一起
- javascript - 控制 DOM 中的每个“src”属性更改
- javascript - 未捕获的 ReferenceError: sel 未在 HTMLDivElement.onclick 中定义
- html - 如果元素定位已经受到容器 div 的影响,如何在 CSS 中创建下拉菜单?
- android - 当应用程序因来电事件关闭时,如何发送通知?