I can't get vue testing to work with vue-test-utils and jest. I created a clean new project with vue cli and added jest as follows, maybe someone can follow along and tell me what I'm doing wrong. (I'm following this installation guide: https://vue-test-utils.vuejs.org/installation/#semantic-versioning)
vue create jest-test1.1.
npm installnpm install --save-dev jest @vue/test-utils vue-jestAdded jest config to package.json:
{ "jest": { "moduleFileExtensions": [ "js", "json", "vue" ], "transform": { ".*\\.(vue)$": "vue-jest" } } }npm install --save-dev babel-jest @babel/core @babel/preset-env babel-core@^7.0.0-bridge.0Adjusted jest config to:
{ "jest": { "transform": { // process `*.js` files with `babel-jest` ".*\\.(js)$": "babel-jest" //<-- changed this } } }- Adjusted babel config to:
module.exports = { presets: [ '@vue/cli-plugin-babel/preset', '@babel/preset-env' //<-- added this ] };Created example.test.js in a tests directory under the project root (jest-test/tests)
Added the following to this file:
import { mount } from '@vue/test-utils' import HelloWorld from "@/components/HelloWorld"; test('displays message', () => { const wrapper = mount(HelloWorld) expect(wrapper.text()).toContain('Welcome to Your Vue.js App') })Added the following to the package.json scripts:
"jest": "jest"npm run jestGet the following error:
C:\Users\xxx\jest-test\tests\example.test.js:1 import { mount } from '@vue/test-utils' ^^^^^^ SyntaxError: Cannot use import statement outside a module
Same happens with Mocha or if I try it in an existing project. Is this a bug? I can't get it working, no matter what I do.
Edit: If I do it with Vue CLI, it works https://vue-test-utils.vuejs.org/installation/#installation-with-vue-cli-recommended