'Jest test fails due to mock function despite its execution

The following Jest unit test:

const onSuccess = jest.fn(() => console.log("Boooboooo"));
render(
  <Footer onSuccess={onSuccess}
  />
);
const approveButton = screen.getByRole("button", {name: "APPROVE"});
userEvent.click(approveButton);
expect(onSuccess).toHaveBeenCalled();

fails:

expect(jest.fn()).toHaveBeenCalled()

Expected number of calls: >= 1
Received number of calls:    0

whereas onSuccess does run:

console.log
Boooboooo

The last two parts are from Jest logs. Any idea what's the issue?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source