0

根据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.js2 行故障线路是:

// @ts-check
import swal from 'sweetalert2/dist/sweetalert2.min.js';

这个包装器的开发者添加了一个index.d.ts文件

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

但似乎没有考虑在内。

4

1 回答 1

1

解决了 ...

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

     "jest": {
    ....
    "transformIgnorePatterns": [
      "/node_modules/(?!vue-sweetalert2).+\\.js$"
    ],
    ....
  }
于 2018-10-30T09:32:48.437 回答