如何使用 Jest 编写一个调用resetTimer
并检查startTimer
也被调用的测试?
代码:
setup () {
const startTimer = () => {
// ...
};
const resetTimer = () => {
startTimer();
};
return {
startTimer,
resetTimer
}
测试:
import { shallowMount } from '@vue/test-utils';
import Overlay from '@/components/Overlay.vue';
const wrapper = shallowMount(Overlay);
it('resetTimer should call startTimer', () => {
const spy = jest.spyOn(wrapper.vm, 'resetTimer');
wrapper.vm.startTimer();
expect(spy).toHaveBeenCalled();
});
结果:
TypeError: object.hasOwnProperty is not a function
187 |
188 | it('resetTimer should call startTimer', () => {
> 189 | const spy = jest.spyOn(wrapper.vm, 'resetTimer');
| ^
190 | wrapper.vm.startTimer();
191 | expect(spy).toHaveBeenCalled();
192 | });
谢谢!