Home > Back-end >  How can i test the promise function at jest with react testing library?
How can i test the promise function at jest with react testing library?

Time:05-24

function that i am trying to test

I have this function and i am trying to test it here:

enter image description here

I am trying to make the onClose function callable, but I get an error at this line => expect(onClose).toHaveBeenCalled();

CodePudding user response:

Since your onClose function is not called directly but asyncronously, after some other action you should use the react testing library async API waitFor method which depicts that you expect something to eventually happen.

Your expectation should look like this

await waitFor(() => expect(onClose).toHaveBeenCalled());

Also don't forget to wrap your testing block with async in order to be able to use await.

  • Related