类型错误:(0,_testUtils.createLocalVue)不是一个函数

doj*_*doj 4 javascript unit-testing vue.js jestjs

这是我的代码。请有人帮我找出错误。我正在使用 jest 来测试我使用 Vue 构建的前端。行 const localVue = createLocalVue(); 给出错误 TypeError: (0 , _testUtils.createLocalVue) 不是函数

import { createLocalVue,shallowMount  } from '@vue/test-utils'
import Vuex from 'vuex'
import getters from '../../src/store/module/auth/getters.js'
import TheHeader from '@/components/layout/TheHeader.vue'



// const store = new Vuex.Store({
//     state: {
//         user:null,
//         token:'',
//         expiresIn:null,
//         isUserLoggedIn:false,
//         isAdminLoggedIn:false,
//     }
//   })


describe('TheHeader', () => {
  const localVue = createLocalVue();
  localVue.use(Vuex);
  let store
  let state
 
  it('Checks whether the login is correctly displayed', () => {
    const cmp = shallowMount(TheHeader, { store,localVue})
    expect(cmp.name()).toMatch('TheHeader')
    expect(cmp.vm.isLoggedIn()).toBe(false)
  })

})

Run Code Online (Sandbox Code Playgroud)

ton*_*y19 7

createLocalVue在 的版本 2 中被删除@vue/test-utils,这解释了为什么它在您的示例中未定义。

import Vuex from 'vuex'
import { shallowMount } from '@vue/test-utils'
import TheHeader from '@/components/TheHeader.vue'

const store = /* Vuex store */

const cmp = shallowMount(TheHeader, {
  global: {
    plugins: [Vuex],

    // OR:
    mocks: {
      $store: store,
    }
  }
})
Run Code Online (Sandbox Code Playgroud)