最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: (0 , _testUtils.createLocalVue) is not a function - Stack Overflow

programmeradmin5浏览0评论

This is my code. Can some please help me figure out the error.I am using jest to test out my frontend which I have built using Vue.The line const localVue = createLocalVue(); is giving out the error TypeError: (0 , _testUtils.createLocalVue) is not a function

import { createLocalVue,shallowMount  } from '@vue/test-utils'
import Vuex from 'vuex'
import getters from '../../src/store/module/auth/getters.js'
import TheHeader from '@/ponents/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)
  })

})

This is my code. Can some please help me figure out the error.I am using jest to test out my frontend which I have built using Vue.The line const localVue = createLocalVue(); is giving out the error TypeError: (0 , _testUtils.createLocalVue) is not a function

import { createLocalVue,shallowMount  } from '@vue/test-utils'
import Vuex from 'vuex'
import getters from '../../src/store/module/auth/getters.js'
import TheHeader from '@/ponents/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)
  })

})

Share Improve this question edited May 20, 2022 at 8:56 snoob dogg 2,8854 gold badges36 silver badges65 bronze badges asked Apr 3, 2021 at 15:29 dojdoj 1561 silver badge7 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 9

createLocalVue was removed in version 2 of @vue/test-utils, which explains why it's undefined in your example.

  • To install a Vue plugin (such as Vuex), use the global.plugins mounting option

  • To mock instance APIs (such as this.$store), use the global.mocks mounting option

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

const store = /* Vuex store */

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

    // OR:
    mocks: {
      $store: store,
    }
  }
})
import { mount } from '@vue/test-utils'
import { createApp } from 'vue'
import { createStore } from 'vuex'
import App from '@/views/Home'

creating a fake store

const store = createStore({
  state() {
    return {
      count: 0,
      user: {},
    }
  },
  mutations: {
    increment(state) {
      state.count += 1
    },
  },
})

Creating the Component

const app = createApp(App)
app.use(store)
let wrapper
beforeEach(() => {
  wrapper = mount(App, {
    global: {
      plugins: [store],
    },
    puted: { showAlert: () => false },
  })
})

now you can do the test

test('Home', async () => {
  expect(wrapper.vm.showAlert).toBe(false)
})
发布评论

评论列表(0)

  1. 暂无评论