Vue vue-sweetalert2。测试错误:开玩笑的单位

时间:2018-10-30 08:31:00

标签: unit-testing vue.js jestjs vue-sweetalert2

根据vue-sweetalert2文档,在我的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国际剑联

import Vue, { PluginObject, PluginFunction } from 'vue';
import * as swal from 'sweetalert2';
...

但似乎没有考虑到这一点。

1 个答案:

答案 0 :(得分:0)

已解决...

我在package.json的Jest配置中添加了一个transformIgnorePatterns

     "jest": {
    ....
    "transformIgnorePatterns": [
      "/node_modules/(?!vue-sweetalert2).+\\.js$"
    ],
    ....
  }