【发布时间】:2019-09-08 20:12:27
【问题描述】:
我正在对组件进行一些单元测试。但是,在某些组件中,我在 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()
})
【问题讨论】:
标签: vue.js vuejs2 jestjs babel-jest vue-test-utils