unit-testing - Vue vue-sweetalert2 。测试中的错误:带有 Jest 的单元
问题描述
根据vue-sweetalert2文档,在我的 中main.js
,我导入并使用了插件:
import VueSweetalert2 from "vue-sweetalert2";
Vue.use(VueSweetalert2);
在我的组件中ContactForm.vue
,我可以使用:
this.$swal(...)
但是,当我 test:unit 这个组件时,我需要添加导入和Vue.use()
import VueSweetalert2 from "vue-sweetalert2";
Vue.use(VueSweetalert2);
我得到一个错误:
$ vue-cli-service test:unit ContactForm.spec.js
FAIL tests/unit/ContactForm.spec.js ● 测试套件运行失败
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:
/Users/yves/Developments/WIP/VUE.JS-cli-3/3-chocha-home-content/chocha/node_modules/vue-sweetalert2/src/index.js:2
import swal from 'sweetalert2/dist/sweetalert2.min.js';
^^^^^^
SyntaxError: Unexpected token import
有什么问题?
更新
第vue-sweetalert2/src/index.js
2 行故障线路是:
// @ts-check
import swal from 'sweetalert2/dist/sweetalert2.min.js';
这个包装器的开发者添加了一个index.d.ts
文件
import Vue, { PluginObject, PluginFunction } from 'vue';
import * as swal from 'sweetalert2';
...
但似乎没有考虑在内。
解决方案
解决了 ...
我在 package.json 的 jest 配置中添加了一个 transformIgnorePatterns
"jest": {
....
"transformIgnorePatterns": [
"/node_modules/(?!vue-sweetalert2).+\\.js$"
],
....
}
推荐阅读
- networking - 将网站设置为 Internet 流量,无法转发端口或对 raspberryPi 使用本地 ssh
- python - 带有 Pyinstaller 的 Pywinauto 提供 KeyError “格式化程序”
- python - 需要更多解释如何实现这个功能计数器
- html - 带有总和边界线和填充的表格?
- python-3.x - 在 TensorFlow 1.15 的自定义训练循环中将张量转换为 numpy 数组
- junit - Mono.map() 中未调用模拟类
- logic - 如何以编程方式求解方程,如 <=0 意味着 >0
- android - 升级到最新的 android/firebase sdk,这个 azure 通知中心示例 - xamarin 表单
- go - 为什么不能在 envoy okhttpresponse 中设置响应体字段?
- node.js - GUILD_VOICE_STATES 是什么意思以及如何使用?