How to retry an expect statement if it fails in jest

Viewed 1360

I am using jest to do unit testing in my react application.

I have a material ui date picker in my application with next and previous buttons and I have the current selected date in the browser url. Whenever user clicks next or previous button, the url gets updated. I used history.push to do that and I checked that it's working properly in the ui. But the test case I have to verify this is failing at the following expect statement expect(history.push).toBeCalledWith('/path/to/url?date=${date}'); saying 0 calls are made to this function. I want to retry only this particular expect statement a couple more times if it fails to make sure that it's not failing due to some delay. How can I do that?

retry(expect(history.push).toBeCalledWith('/path/to/url?date=${date}'), numTimes);

1 Answers

You can simply just use a setTimeout with a callback of your expect statements and a jest done() after. Set the delay to your liking and just be sure you've mocked the history object properly.

Related