1

Error:

The "actual" argument in expect(actual).toHaveBeenCalled() must be a spy

Minimal not-working example:

const mockFn = jest.fn();
mockFn();
expect(mockFn).toHaveBeenCalled();

Question: is jest.fn() not a spy?

CherryQu
  • 3,343
  • 9
  • 40
  • 65

1 Answers1

0

I tried the below implementation which worked for me,

it("should call mock function when button is clicked", () => {
    const mockFn = jest.fn();
    const tree = shallow(<button name="button test" handleHandler={mockFn} />);
    tree.simulate("click");
    mockFn();
    expect(mockFn).toHaveBeenCalled();
  });
Amjad Khan
  • 61
  • 1
  • 3
  • Hey, I have a question, why did you call `mockFn();` before the expect statement? – AKJ Dec 21 '18 at 02:24