Ada*_*ler 6 unit-testing vue.js jestjs vuex vuejs2
我正在为 VueJS 组件编写单元测试,并查阅了Vue Test Utils Common Tips的“应用全局插件和 Mixins”部分。我有一个依赖于 Vuex 存储的组件,因此出于我的目的,我将转置该部分下的示例是有意义的。
这是该组件的特定 .spec.js 文件的代码:
import { createLocalVue, mount } from '@vue/test-utils'
import AppFooter from '@/components/AppFooter/AppFooter'
import store from '@/store'
describe('AppFooter component', () => {
const localVue = createLocalVue()
localVue.use(store)
it('AppFooter should have header slot', () => {
const AppFooterComponent = mount(AppFooter, {
localVue
})
/* TODO: Replace with a more appropriate assertion */
expect(true).toEqual(true)
})
})
Run Code Online (Sandbox Code Playgroud)
这非常忠实于上面链接中提供的示例。但是,当我运行测试套件时收到的错误如下:
我应该以不同的方式安装 Vue 商店吗?
为了详细说明我的评论,我认为它应该如下所示,您在 mount() 调用中传递 store 。
import { createLocalVue, mount } from '@vue/test-utils'
import AppFooter from '@/components/AppFooter/AppFooter'
import Vuex from 'vuex'
import store from '@/store' //you could also mock this out.
describe('AppFooter component', () => {
const localVue = createLocalVue()
localVue.use(Vuex)
it('AppFooter should have header slot', () => {
const AppFooterComponent = mount(AppFooter, {
store,
localVue
})
/* TODO: Replace with a more appropriate assertion */
expect(true).toEqual(true)
})
})
Run Code Online (Sandbox Code Playgroud)