Repeat the same test and the entire project to find flakey tests
Recently I have looked at the reports of cy.route command failing to intercept a network request made by the web application. The following code failed sometimes according to the user:
1 2 3 4 5 6 7 8 9 10 11 12 13
describe("cypress bug", () => { it("should fail sometimes, rerun if not ;)", () => { cy.server(); cy.route("get", "/api/posts").as("getPosts");
When trying the test locally, I do not see a problem. In the video below I am repeatedly re-running the test again and again - and it always passes.
Hmm, seems the project works in cypress open mode. Maybe the test fails or is unreliably during cypress run mode? To run the same spec again and again during cypress run we can use cypress-repeat utility, one of my Cypress wrappers. Let's install it
1 2 3
$ yarn add -D cypress-repeat info Direct dependencies └─ [email protected]
In package.json instead of cypress run we can use cypress-repeat run. Let's run the spec 10 times
Ok, the project does seem to pass 10 times in the row in cypress run mode. Maybe the flake is rarer than that.
Instead of repeating a project with a single test, let's repeat the same test 100 times. We can use the bundled Lodash to create test functions on the fly.
Bingo! One of the tests failed. So there is some flake. Let's see if we can solve the flake problem by switching from the deprecated cy.route to much more powerfulcy.intercept command. We no longer need cy.server() call.
1 2 3 4 5 6 7 8 9 10
it(`test ${k} of 100`, () => { cy.intercept("GET", "/api/posts").as("getPosts");
Finally, let's run the same 100 tests 10 times in a row using cypress-repeat command from the terminal. We can use npm test or call cypress-repeat straight using NPX/Yarn
1 2 3 4 5 6 7 8
$ yarn cypress-repeat run -n 10 yarn run v1.22.5 $ /Users/gleb/git/cypress-bug/node_modules/.bin/cypress-repeat run -n 10 cypress-repeat: will repeat Cypress run 10 time(s) ***** cypress-repeat: 1 of 10 ***** ... ***** finished 10 run(s) successfully ***** ✨ Done in 271.29s.
The run completes 10 * 100 = 1000 tests in a row. Seems the test is now flake-free.
Tip: if this test shows very low frequency of flake, for example < 1%, I would simply add the test retries to repeat it automatically.
1 2 3 4
// repeat the test if it fails, up to 3 additional attempts it(`test ${k} of 100`, {retries: 3}, () => { ... })