Unexpected token error occurs when using map-spread operator in vue-test-utils combined with jest

I recently set up testing for my Vue project by following the instructions provided in this helpful guide here

Upon completion of the guide, I proceeded to create a test for one of my components. However, when I ran jest, I encountered the following error:

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

Despite conducting research on this error and examining other sample projects, I have yet to determine how to resolve it.

Your assistance regarding this matter would be greatly 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"
    }
  }

Answer №1

Refer to this helpful answer for resolving the SyntaxError related to the spread operator: SyntaxError on using spread operator with babel env preset

To fix this issue, make sure to install babel-plugin-transform-object-rest-spread by running:


npm install --save-dev babel-plugin-transform-object-rest-spread

Next, add the plugin under the "plugins" option in your .babelrc file:


"plugins": ["transform-object-rest-spread"]

For mocking getters in your test, check out the guide at https://vue-test-utils.vuejs.org/guides/#mocking-getters.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Implementing a validator based on the operator selection in react-querybuilder

Is it possible to add validators to fields that are only active when using the like operator? For example, if the like or unlike operators are used in the phoneNumber field without the % sign, it should be invalid. If the = operator is used, the % sign sho ...

"I am looking for a way to retrieve dynamic data from a (click) event in Angular. Can

Within my component, I have a video loaded in an i tag on a click event. The challenge is accessing the video ID from the video.component.ts file in order to dynamically load a new video. The solution has been elusive so far. <li *ngFor="let video of c ...

Nuxt Authentication with Dual Local Endpoints

I am interested in creating two local endpoints like the ones below: strategies: { localOne: { endpoints: { login: { url: "token/", method: "post", propertyName: "access" }, user: { url: "user/me/", method: "get", propertyName: false }, ...

Customizing the renderInput of the Material UI DatePicker

Recently I integrated material-ui into my React project with TypeScript. I implemented the following code based on the example provided on the official website. import AdapterDateFns from '@mui/lab/AdapterDateFns'; import DatePicker from '@m ...

Converting RGBA to Hex Color Code with Javascript: A Step-by-Step Guide

I attempted to change the rgba color representation to hexadecimal, but I encountered difficulty in converting the opacity value while successfully converting the remaining colors. Here is the snippet of my code: var colorcode = "rgba(0, 0, 0, 0.74)"; ...

Rendering the page using ReactDOM.render

Just started with ReactJS, I'm struggling to figure out why my page isn't displaying anything - <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> ...

What is the best way to retrieve the http.server object from the express application?

Currently, I am in the process of developing a server-side node.js application utilizing express to manage HTTP requests. In addition to this, I am incorporating websockets (socket.io) support into the application. The challenge I face is that I do not hav ...

Using Node.js to retrieve a p12 certificate from the certificate repository

Is there a way to retrieve the p12 certificate from the certificate store after it has been installed? I am facing a situation where both the private key and certificate are combined in a p12 certificate and then stored in the Windows certificate store. ...

React Switch not displaying various pages correctly

After creating a new component to switch between pages on my React app, I encountered an issue where the HomePage renders correctly when entering the site, but clicking on navlinks does not work. Additionally, when trying to access the url /contacto, ins ...

The ajax function nestled within a nested forEach loop completes its execution once the inner callback function is triggered

I am encountering a problem with an ajax call that is nested within a forEach loop inside another function. The issue is that the callback of the outer function is being triggered before the inner loop completes, resulting in "staticItemList" not being p ...

What is the best approach to modifying array elements using onChange and hooks in React?

I'm struggling to implement something simple, and the solutions I've found so far don't quite address my specific issue. Here's the state in question: const [formFields, setFormFields ] = useState({ formTitle: '', fiel ...

Issue encountered while initializing session_start() in PHP REACTJS AXIOS

https://i.sstatic.net/bSelX.pngWhen attempting to log in to my web application, I encountered an issue where the session is not opening after entering the correct email and password. Strangely, there is no PHPSESSID present in the console > application. ...

ng-bind-html is having trouble parsing the HTML correctly and binding it

Here is the code for my controller: myApp.controller('actionEditController', ['$scope', '$stateParams', '$sce',function ($scope, $stateParams, $sce) { $scope.table="<p>OOPSY</p>"; $sc ...

``When you click, the image vanishes into thin

Could you please explain why the image disappears once I close the lightbox? Access with password: chough ...

Issues displaying images when using Laravel and Vue.js from a subdirectory

Currently, I am facing an issue with the display of images in my vuejs project located in a subdirectory. The images from the public/images/ directory are not showing up. Despite trying numerous solutions, nothing seems to work for me. Strangely, the image ...

Incorrect handling of Unix timestamps in AngularJS

I'm facing an issue with timestamps. Let me explain - I have a timestamp coded as 1378028575, which corresponds to Sun, 01 Sep 2013 09:42:55 GMT according to this website. The problem arises when I attempt to format this timestamp using Angular's ...

The issue with updating values in the jQuery UI datepicker persists

When using the jquery datepicker ui, you may notice that the value attributes of the associated html fields do not update immediately. For example: http://jsfiddle.net/4tXP4/ Check out this link for more details: http://jqueryui.com/demos/datepicker/al ...

Reasons for dividing by 1 in JavaScript

While completing a basic programming assignment from my teacher, I encountered an interesting issue in Javascript. I found that when dividing a number by 1, it returns an unexpected value. Can anyone provide an explanation for this? I have created a jsfidd ...

Retrieve data from two databases and display the information from two separate arrays on a single ejs page after making two separate database calls

Currently, I am faced with a challenge where I need to render a page by passing two arrays that are populated by two separate database calls. It seems that when I only pass one array to the ejs page, everything works as expected. For a single array, my a ...

Is it really impossible to post complex JSON parameters with Restangular?

Is it possible to send a complex JSON object to a PUT route using Restangular? Restangular.one('model3ds', model.uuid).put( api_key: "blabla" model3d: { is_public: true } ) However, the data sent by Restangular appears as: ...