jest.fn() not recognized by expect as a spy function?

636 views Asked by At

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?

1

There are 1 answers

1
Amjad Khan On

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