When using Cypress tests, getting some part of your system to work can be tricky. What to do when tests randomly timeout? I've worked on several solutions where it's almost a 50-50% chance that the test will pass or fail. Sometimes it can be that on first-load a page takes too long to load before the test timeouts. Some functionality you want to test might rely on an external API that is outside your control. In a perfect world, we would fix the code to prevent this. However, in the real world, we will sometimes need alternatives. In this tutorial, you will learn how to do that.
Cypress Plugin Retries
The first obvious choice is to simply configure Cypress to have a longer default timeout. This solution is not ideal as usually you only have one or two problem tests and extending the timeout for all tests is unnecessary. If you find yourself in this situation then this is where Cypress Plugin Retries comes in.
To install the plug-in type then within a terminal type:
After the plug-in has been installed. the next step is to add the 'CYPRESS_RETRIES' command within the package.json script section, like so:
Within your project, you will have your own custom script to launch your test. The main thins you need to add is 'CYPRESS_RETRIES=3'. Now when you run your Cypress project, before the build is failed from a single timeout, Cypress will try and run the test 3 times. Granted this approach feels a bit hacky, but it has helped me out a lot in the last few months. Enjoy!