无法读取未定义的属性“getters” - 使用 jest 进行 VueJS 单元测试

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 商店吗?

swa*_*nee 0

为了详细说明我的评论,我认为它应该如下所示,您在 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)