根据vue-甜警报2文档,在我的main.js中,我导入和使用插件:
import VueSweetalert2 from "vue-sweetalert2";
Vue.use(VueSweetalert2);在我的组件ContactForm.vue中,我可以使用:
this.$swal(...)但是,当我测试:单元这个组件时,我需要添加导入和Vue.use()
import VueSweetalert2 from "vue-sweetalert2";
Vue.use(VueSweetalert2);我得到了一个错误:
$ vue-cli-service test:unit ContactForm.spec.js失败测试/单元/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 fie。
import Vue, { PluginObject, PluginFunction } from 'vue';
import * as swal from 'sweetalert2';
...但似乎没有考虑到这一点。
发布于 2018-10-30 09:32:48
解决了..。
我在package.json中的jest配置中添加了一个package.json
"jest": {
....
"transformIgnorePatterns": [
"/node_modules/(?!vue-sweetalert2).+\\.js$"
],
....
}https://stackoverflow.com/questions/53060193
复制相似问题