Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i test the promise function at jest with react testing library?

function that i am trying to test

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

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading