How can I write a test using Jest that invokes resetTimer
and checks that startTimer
is also invoked?
Code:
setup () {
const startTimer = () => {
// ...
};
const resetTimer = () => {
startTimer();
};
return {
startTimer,
resetTimer
}
Test:
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();
});
Result:
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 | });
Thanks!
setup
on the component within the test, to execute thespy
before it returns? – Holle