How to rerun failed specs after cypress run

Note: this question is not related to test retries, this is about running the whole spec again from beginning, not about retrying a failed it() block within a spec.

According to Cypress docs: https://docs.cypress.io/guides/guides/module-api#cypress-run
you can rerun failing spec/specs after the catching the failures of cypress.run() process. I cannot find a way to do so. My current code is as below. Please add necessary steps to rerun failed specs in the same build then eventually fail/exit after a certain amount of failed rerun attempts

await cypress
    .run(runOptions)
    .then(result => {
      let exitCode = 0
      if (result.runs)
        if (result.runs.length > 0) {
          for (let i = 0; i < result.runs.length; i++) {
            for (let j = 0; j < result.runs[i].tests.length; j++) {
              if (result.runs[i].tests[j].state === 'failed') {
                // any logic here to rerun the failed specs?
                // may be a recursion fuction to call cypress.run() again with failed specs in the runOptions?
              }
            }
          }
        }
      process.exitCode = exitCode
    })
    .catch(err => {
      console.error(err.message)
      process.exit(1)
    })

Source: Ask Javascript Questions

LEAVE A COMMENT