admin管理员组文章数量:1313598
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();
});
本文标签: javascriptjest function must be a mock or spyStack Overflow
版权声明:本文标题:javascript - jest function must be a mock or spy - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741929097a2405469.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论