Vue vue-sweetalert2 . error in test:unit with Jest

1.2k Views Asked by At

As per the vue-sweetalert2 doc, in my main.js, I import and use the plugin:

import VueSweetalert2 from "vue-sweetalert2";
Vue.use(VueSweetalert2);

In my component, ContactForm.vue, I can use:

this.$swal(...)

However, when I test:unit this component, I need to add the import and Vue.use()

 import VueSweetalert2 from "vue-sweetalert2";
 Vue.use(VueSweetalert2);

and I get an error:

$ vue-cli-service test:unit ContactForm.spec.js

FAIL tests/unit/ContactForm.spec.js ● Test suite failed to run

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

What could be wrong?

UPDATE

the vue-sweetalert2/src/index.js, line 2 faulty line, is:

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

the developer of this wrapper added an index.d.ts fie

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

but it seems not to be taken in account.

1

There are 1 best solutions below

0
AudioBubble On

SOLVED ...

I added a transformIgnorePatterns in my jest config in package.json

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