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 :
JavaScript
x
5
1
methodA({ dispatch, commit }, { data }) {
2
dispatch('methodB', { data });
3
}
4
};
5
My unit test :
JavaScript
1
8
1
describe('UnitTesting', () => {
2
it('if method called', () => {
3
const commit = jest.fn();
4
const dispatch = jest.fn('methodB');
5
service.actions.methodA({ dispatch, commit });
6
expect(dispatch).toHaveBeenCalledTimes(1);
7
});
8
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.