How can a jest test waitFor a mocked function not to have been called?

Viewed 22

Given an implementation that looks something like the following; in a unit test, how can one assert that a mock of someExternalCallback is never called?

const MyComp = ({ isLoading, children }) => {
  useEffect(() => {
    if (!isLoading) {
        someExternalAsyncFunction().then(someExternalCallback);
    }
  }, [isLoading]);

  return <>{children}</>;
}

Assuming that both external functions are mocked in the test file, and there's a test that looks like the following, how can the inverse be tested?

it("should call someExternalCallback when [...]", async () => {
  // stuff

  await waitFor(() => expect(someExternalCallbackMock).toHaveBeenCalledTimes(1));
});

For the inverse, I've currently written a test that looks like the following.

it("should never call someExternalCallback when [...]", async () => {
  // different stuff

  await waitFor(() => expect(someExternalCallbackMock).not.toHaveBeenCalled());
});

But I'm assuming the combination of waitFor and .not.toHaveBeenCalled doesn't actually test the intended situation. My understanding is that waitFor calls the callback it's given many times until it passes, but in this situation the test wants to assert that something never happens.

0 Answers
Related