How can I retry a failed test?

Cypress 5 now has a native support for retries. Check: https://cypress.io/blog/2020/08/19/introducing-test-retries-in-cypress-5-0


Update (v5.0.0)

Cypress now has built-in retry support.

You can set test retries in Cypress 5.0 via configuration in cypress.json

{
  "retries": 1
}

or specify different options for runMode and openMode:

{
  "retries": {
    "runMode": 1,
    "openMode": 3
  }
}

runMode allows you to define the number of test retries when running cypress run

openMode allows you to define the number of test retries when running cypress open

You can turn on test retries for just a single test or suite via test options:

it('my test', {
  retries: 2
}, () => {
  // ...
})

// or

describe('my suite', {
  retries: 2
}, () => {
  // ...
})

If a test fails in a beforeEach, afterEach, or in the test body, it will be retried. Failures in beforeAll and afterAll hooks will not retry.


Old answer:

Official Support for test retries is on the way, but there's a plugin for that. cypress-plugin-retries

Disclosure: I'm the creator of the plugin.

Installation

Add the plugin to devDependencies

npm install -D cypress-plugin-retries

At the top of cypress/support/index.js:

require('cypress-plugin-retries')

Usage

Use the environment variable CYPRESS_RETRIES to set the retry number:

CYPRESS_RETRIES=2 npm run cypress

or use Cypress.env('RETRIES') in your spec file:

Cypress.env('RETRIES', 2)

or on a per-test or per-hook basis, set the retry number:

Note: this plugin adds Cypress.currentTest and you should only access it in the context of this plugin.

it('test', () => {
    Cypress.currentTest.retries(2)
})

Note: Please refer to this issue for updates about official cypress retry support