3

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"
    }
  }
DrevanTonder
  • 726
  • 2
  • 12
  • 32

1 Answers1

1

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.

Zosimov
  • 141
  • 1
  • 4
  • That works, but there some warnings about ... (spread) operator, to fix it [newer version of the plugin](https://github.com/babel/babel/issues/8263#issuecomment-402546923) is required in your package.json – dajnz Feb 12 '20 at 20:00