I'm using the composition API in vue2, and trying to write a unit test that spys on a function to make sure another function has called it, but the test fails saying that my spy hasn't been called. Adding a console log in my actual component functions, I can see it is called as expected though. Codesandbox of the issue
Error:
Expected number of calls: >= 1
Received number of calls: 0
Component function:
export default defineComponent({
name: 'PersonsReport',
setup() {
// other methods and vars
const closeReportModal = (): void => {
isReportModalOpen.value = false;
resetSearchDates();
};
const resetSearchDates = () => {
console.log('in resetSearchDates') // this fires in the test
}
return {
closeReportModal,
resetSearchDates
}
}
})
My test:
import Vue from 'vue';
import { mount } from '@vue/test-utils';
import CompositionApi from '@vue/composition-api';
import PersonsReport from '~/components/persons/PersonsReport.vue';
Vue.use(CompositionApi);
let wrapper: any;
const wrapperBeforeEach = () => {
wrapper = mount(PersonsReport);
};
describe('PersonsReport', () => {
beforeEach(async () => {
wrapperBeforeEach();
});
test('closeReportModal to call resetSearchDates', async () => {
const spy = jest.spyOn(wrapper.vm, 'resetSearchDates');
await wrapper.vm.$nextTick();
wrapper.vm.closeReportModal();
await wrapper.vm.$nextTick();
expect(spy).toHaveBeenCalled();
spy.mockRestore();
});
});