我正在对组件进行单元测试。然而,在某些组件中,我有一些在mounted
钩子上运行的内容使我的测试失败。
我已经成功地模拟了我不需要的方法。但是,我想知道是否有一种解决方案可以模拟mounted
钩子本身。
@/components/attendeesList.vue
<template>
<div>
<span> This is a test </span>
</div>
</template>
JS
<script>
methods: {
testMethod: function() {
// Whatever is in here I have managed to mock it
}
},
mounted: {
this.testMethod();
}
</script>
Test.spec.js
import { mount, shallowMount } from '@vue/test-utils'
import test from '@/components/attendeesList.vue'
describe('mocks a method', () => {
test('is a Vue instance', () => {
const wrapper = shallowMount(attendeesList, {
testMethod:jest.fn(),
})
expect(wrapper.isVueInstance()).toBeTruthy()
})