我试图在Vue的mounted()
生命周期钩子中为逻辑编写单元测试,但没有太多运气。问题似乎是当使用vue-test-utils mounted()
挂载组件时,mount
永远不会被调用。这是我尝试测试的Vue组件:
<template>
<div></div>
</template>
<script>
export default {
name: 'MyComponent',
mounted () {
this.$store.dispatch('logout')
}
}
</script>
测试本身:
import { mount, createLocalVue } from '@vue/test-utils'
import Vuex from 'vuex'
import MyComponent from '@/components/MyComponent'
const localVue = createLocalVue()
localVue.use(Vuex)
describe('MyComponent.vue', () => {
let store
let actions
beforeEach(() => {
actions = {
logout: jest.fn().mockName('logout')
}
store = new Vuex.Store({
state: {},
actions
})
})
it('calls store "logout" action', () => {
mount(MyComponent, { localVue, store })
expect(actions.logout).toHaveBeenCalled()
})
})
但是,这会因expect(logout).toHaveBeenCalled()
断言错误而失败。
如果我直接用actions.logout()
调用模拟商店操作,测试通过了,我还有其他测试也可以调用商店操作,例如按下按钮,那些也会通过,所以问题肯定会出现使用mounted()生命周期钩子。
有什么想法吗?
(vue 2.5.4
和vue-test-utils 1.0.0-beta-.15
)
答案 0 :(得分:0)
不知道它有什么不同,但是我将商店模拟抽象到另一个文件,一切似乎都可以正常工作了。
mocks.js
export const storeMock = Object.freeze({
state: {},
actions: {
logout: jest.fn().mockName('logout')
},
})
test.spec.js
import { shallowMount, createLocalVue } from '@vue/test-utils'
import Vuex from 'vuex'
import { storeMock } from './mocks.js'
import MyComponent from '@/components/MyComponent'
const localVue = createLocalVue()
localVue.use(Vuex)
describe('MyComponent.vue', () => {
let options
beforeEach(() => {
jest.clearAllMocks()
const store = new Vuex.Store(storeMock)
options = { store, localVue }
})
it('calls store "logout" action', () => {
shallowMount(MyComponent, options)
expect(storeMock.actions.logout).toHaveBeenCalled()
})
})
答案 1 :(得分:0)
没有将商店模拟抽象到另一个文件,没有 beforeEach
的方法略有不同(由于某种原因破坏了我的测试)。
import { createLocalVue, shallowMount } from "@vue/test-utils";
import Vuex from "vuex";
import MyComponent from "@/components/MyComponent.vue";
describe("MyComponent", () => {
const localVue = createLocalVue();
localVue.use(Vuex);
const actions = {
logout: jest.fn()
};
const store = new Vuex.Store({ actions });
const wrapper = shallowMount(MyComponent, {
localVue,
store
});
it('calls store "logout" action', () => {
expect(actions.logout).toHaveBeenCalled();
});
});