I'm struggling to get Vue testing set up with vue-test-utils and jest. I followed the installation guide at https://vue-test-utils.vuejs.org/installation/#semantic-versioning, but can't seem to figure out what's wrong. Here's what I've done so far:
vue create jest-test
1.1.
npm install
npm install --save-dev jest @vue/test-utils vue-jest
Added 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.0
Adjusted jest config to:
{ "jest": { "transform": { // process `*.js` files with `babel-jest` ".*\\.(js)$": "babel-jest" } } }
- Adjusted babel config to:
module.exports = { presets: [ '@vue/cli-plugin-babel/preset', '@babel/preset-env' ] };
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 jest
Encountering the 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
I face the same issue with Mocha or when attempting it on an existing project. Is there a bug here? I'm at a loss, any help would be appreciated.
Edit: It seems to work fine when using Vue CLI https://vue-test-utils.vuejs.org/installation/#installation-with-vue-cli-recommended