I am currently performing unit tests on my application as seen. But I have a problem. How to mock or exploit the dispatch of Vuex ?
My method to test :
methodA({ dispatch, commit }, { data }) { dispatch('methodB', { data }); } };
My unit test :
describe('UnitTesting', () => { it('if method called', () => { const commit = jest.fn(); const dispatch = jest.fn('methodB'); service.actions.methodA({ dispatch, commit }); expect(dispatch).toHaveBeenCalledTimes(1); });
I have a error message : Dispatch is not a function. Why ? Do you help me please ? I don’t understand why I had this message.
thanks
Advertisement
Answer
jest.fn('methodB')
is not not a proper call, thus the resulting dispatch
constant not being a function.
The fn
parameter should be a function.
Either do jest.fn(() => Promise.resolve('functionB'))
or jest.fn().mockResolvedValue('functionB')
But just jest.fn()
would make it here.