当我尝试测试Logout.vue组件时,出现与“调度”有关的错误。
GitHub存储库:https://github.com/SergioFerrera/jest_vuejs
错误代码:
TypeError: Cannot read property 'push' of undefined
我尝试从此处复制代码:https://medium.com/@lachlanmiller_52885/mocking-vuex-in-vue-unit-tests-b6eda1c4d301
logout.test.js
import Vuex from 'vuex'
import { shallow, createLocalVue } from 'vue-test-utils'
import Logout from "../../src/app/components/Logout.vue";
const localVue = createLocalVue()
localVue.use(Vuex)
describe('Logout.vue', () => {
let store
let logout
beforeEach(() => {
logout = {
destroyToken: jest.fn()
}
store = new Vuex.Store({
logout
})
})
it('comprobando llamadas a destroyToken', () => {
const wrapper = shallow(Logout, {
store,
localVue
})
expect(logout.destroyToken.mock.calls).toHaveLength(1)
})
})
Logout.vue
<template>
</template>
<script>
export default {
created(){
this.$store.dispatch('destroyToken')
.then(response => {
this.$router.push({ name: 'Inicio'})
})
}
}
</script>