Efficiently Re-Running Failed Cypress Tests Using Mochawesome and Dynamic Configuration
End-to-End (E2E) testing with Cypress can often be hampered by flaky tests that pass intermittently due to network issues, timing problems, or environmental inconsistencies. Re-running an entire test suite each time a test fails is time-consuming, especially for large projects. This article presents a systematic approach to efficiently re-run only failed Cypress tests. By leveraging Mochawesome, developers can generate detailed JSON and HTML reports of test results. A Node.js script can then parse these reports to extract failed test titles and their corresponding spec files, producing a failed-tests.json manifest. The @cypress/grep plugin allows dynamic test filtering based on this manifest, avoiding common command-line limitations like ENAMETOOLONG errors. A dedicated run-failed-tests.js script injects the necessary configuration into Cypress, executes the failed tests, and restores the original configuration to maintain a clean state. Additionally, best practices such as clearing Cypress cache and resetting application state between runs ensure reliable results. The workflow is integrated into package.json scripts, simplifying execution for full test runs or selective re-runs. This approach significantly reduces CI/CD runtime, improves feedback speed, and streamlines test management, transforming test recovery from a manual task into an automated, efficient process.
