简体   繁体   中英

jest function must be a mock or spy

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

You 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();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM