I'm writing a test where Im testing the actions in my app. I'm having trouble trying to get the last expect to be called.
const pushData = jest.fn(() => Promise.resolve());
test('anotherAsyncCall is fired to get more info', () => {
const store = mockStore({});
asynCallToGetData = jest.fn(() => Promise.resolve());
const action = pushData();
const dispatch = jest.fn();
const anotherAsyncCall = jest.fn(() => Promise.resolve());
const expectedActions = [{
type: 'DATA_RECEIVED_SUCCESS'
}];
return store.dispatch(action).then(() => {
expect(asynCallToGetData).toHaveBeenCalled();
expect(store.getActions()).toMatch(expectedActions[0].type);
expect(dispatch(anotherAsyncCall)).toHaveBeenCalled(); //This fails
});
});
But the message I get after running test is
expect(jest.fn())[.not].toHaveBeenCalled()
jest.fn() value must be a mock function or spy.
Received: undefined here
I'm writing a test where Im testing the actions in my app. I'm having trouble trying to get the last expect to be called.
const pushData = jest.fn(() => Promise.resolve());
test('anotherAsyncCall is fired to get more info', () => {
const store = mockStore({});
asynCallToGetData = jest.fn(() => Promise.resolve());
const action = pushData();
const dispatch = jest.fn();
const anotherAsyncCall = jest.fn(() => Promise.resolve());
const expectedActions = [{
type: 'DATA_RECEIVED_SUCCESS'
}];
return store.dispatch(action).then(() => {
expect(asynCallToGetData).toHaveBeenCalled();
expect(store.getActions()).toMatch(expectedActions[0].type);
expect(dispatch(anotherAsyncCall)).toHaveBeenCalled(); //This fails
});
});
But the message I get after running test is
expect(jest.fn())[.not].toHaveBeenCalled()
jest.fn() value must be a mock function or spy.
Received: undefined here
Share
Improve this question
edited Mar 6, 2020 at 20:20
Corbuk
7101 gold badge8 silver badges25 bronze badges
asked Feb 23, 2018 at 14:19
FranWalkerFranWalker
311 gold badge1 silver badge3 bronze badges
1 Answer
Reset to default 6You should use jest.spyOn(object, methodName) to create Jest mock function. For example:
const video = require('./video');
test('plays video', () => {
const spy = jest.spyOn(video, 'play');
const isPlaying = video.play();
expect(spy).toHaveBeenCalled();
expect(isPlaying).toBe(true);
spy.mockReset();
spy.mockRestore();
});