DrevanTonder
DrevanTonder

Reputation: 717

vue-test-utils with jest throws unexpected token error for map-spread operator

I was trying to set up testing for my Vue project following this guide https://vue-test-utils.vuejs.org/en/guides/testing-SFCs-with-jest.html

I finished the guide and created a test for one of my components. I then ran jest and I got the error below:

unknown: Unexpected token (10:4)
         8 | export default {
         9 |   computed: {
      > 10 |     ...mapGetters([
           |     ^
        11 |       'user'
        12 |     ])
        13 |   }

I have googled this error and looked at other example projects but I have still no idea how to fix this yet.

Any help will be appreciated.

App.vue

<template>
  <div id="app" />
</template>

<script>
  import { mapGetters } from 'vuex'

  export default {
    computed: {
      ...mapGetters([
        'user'
      ])
    }
  }
</script>

App.spec.js

import { shallow } from '@vue/test-utils'
import App from './App'

describe('App', () => {

  it('works', () => {
    const wrapper = shallow(App)

    expect(wrapper.isVueInstance()).toBeTruthy()
  })
})

.babelrc

{
"presets": [
    ["env", { "modules": false }]
  ],
  "env": {
    "test": {
      "presets": [
        ["env", { "targets": { "node": "current" }}]
      ]
    }
  }
}

package.json (just jest part)

"jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "vue"
    ],
    "transform": {
      ".*\\.(vue)$": "<rootDir>/node_modules/vue-jest",
      "^.+\\.js$": "<rootDir>/node_modules/babel-jest"
    },
    "snapshotSerializers": [
      "<rootDir>/node_modules/jest-serializer-vue"
    ],
    "moduleNameMapper": {
      "^@/(.*)$": "<rootDir>/src/$1"
    }
  }

Upvotes: 3

Views: 1425

Answers (1)

Zosimov
Zosimov

Reputation: 151

Via this answer: SyntaxError on spread operator, while using babel env preset

To use the spread operator, you must use babel-plugin-transform-object-rest-spread, so install it: npm install --save-dev babel-plugin-transform-object-rest-spread

And add it under the "plugins" option in .babelrc: "plugins": ["transform-object-rest-spread"]

Also, look at https://vue-test-utils.vuejs.org/guides/#mocking-getters to mock your getters in the test.

Upvotes: 1

Related Questions