带有jest的vue-test-utils会为map-spread运算符抛出意外的令牌错误

时间:2018-05-08 02:44:24

标签: javascript vue.js babeljs jestjs vue-test-utils

我试图按照本指南https://vue-test-utils.vuejs.org/en/guides/testing-SFCs-with-jest.html

为我的Vue项目设置测试

我完成了指南并为我的一个组件创建了一个测试。然后我跑了// // ALL of this operation Run on MainQueue // let allFetchRequest = prepareCommonFetchRequest() allFetchRequest.predicate = NSPredicate.init(format: "name CONTAINS[cd] %@ || mobile_number CONTAINS[cd] %@", searchString, searchString) let tempAllContactSearchFetchResultController = NSFetchedResultsController.init(fetchRequest: allFetchRequest, managedObjectContext: managedObjectContext!, sectionNameKeyPath: "section_key", cacheName: nil) managedObjectContext?.perform { do { try self.allContactSearchFetchResultController?.performFetch() DispatchQueue.main.async { self.allContactSearchFetchResultController = tempAllContactSearchFetchResultController // calling the delegate which will reload the UITableView } } catch { print("Error occur") // TODO // handle error here } } ,我得到了以下错误:

jest

我已经搜索了这个错误并查看了其他示例项目,但我仍然不知道如何解决这个问题。

任何帮助将不胜感激。

App.vue

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

App.spec.js

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

<script>
  import { mapGetters } from 'vuex'

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

.babelrc

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

describe('App', () => {

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

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

package.json(只是开玩笑部分)

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

1 个答案:

答案 0 :(得分:0)

通过这个答案: SyntaxError on spread operator, while using babel env preset

要使用spread运算符,必​​须使用babel-plugin-transform-object-rest-spread,所以安装它: npm install --save-dev babel-plugin-transform-object-rest-spread

并将其添加到&#34;插件&#34; .babelrc中的选项: "plugins": ["transform-object-rest-spread"]

另外,看看https://vue-test-utils.vuejs.org/guides/#mocking-getters在测试中嘲笑你的吸气剂。