Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Crude implementation of retrying failed spec files in Cypress. Now with Parallelization support!
const _ = require('lodash')
const cypress = require('cypress')
const crypto = require('crypto')
* Add your config args in DEFAULT_CONFIG
* Set number of retries in MAX_NUM_RUNS
const MAX_NUM_RUNS = 3
// you can omit 'spec' if you just want all your tests to run
// spec: './cypress/integration/retries/**/*',
browser: "electron",
record: true,
// parallelization options
group: "main",
parallel: true,
// only necessary if recording specs from a local machine
// ciBuildId: crypto.randomBytes(6).toString('hex')
// id unique to the machine
const uniqueId = crypto.randomBytes(3).toString('hex')
let totalFailuresIncludingRetries = 0
const run = (num, spec, retryGroup) => {
num += 1
const config = Object.assign(_.cloneDeep(DEFAULT_CONFIG), {
env: {
numRuns: num,
if (spec) config.spec = spec
if (retryGroup) = retryGroup
.then((results) => {
if (results.totalFailed) {
totalFailuresIncludingRetries += results.totalFailed
// rerun again with only the failed tests
const specs = _(results.runs).filter("stats.failures").map("spec.relative").value()
console.log(`Run #${num} failed.`)
// if this is the 3rd total run (2nd retry)
// and we've still got failures then just exit
if (num >= MAX_NUM_RUNS) {
console.log(`Ran a total of '${MAX_NUM_RUNS}' times but still have failures. Exiting...`)
return process.exit(totalFailuresIncludingRetries)
console.log(`Retrying '${specs.length}' specs...`)
// If we're using parallelization, set a new group name
let retryGroupName
if ( {
retryGroupName = `${}: retry #${num} (${specs.length} spec${specs.length===1?'':'s'} on ${uniqueId})`
// kick off a new suite run
return run(num, specs, retryGroupName)
// kick off the run with the default specs

This comment has been minimized.

Copy link

thasherwin commented Aug 30, 2018

How do you pass arguments? For example the config file or the reporter

I have the following line in my package.json:
cypress open --env configFile=testEnvConfig but changing this to node cypress-retries.js --env configFile=testEnvConfig will not use the config file. The test is executed using localhost which is not the correct host. The correct host is in the config file


This comment has been minimized.

Copy link
Owner Author

Bkucera commented Aug 30, 2018

@thasherwin Lines 21-28 is where you would pass your configuration options


This comment has been minimized.

Copy link

germyjen commented Oct 24, 2018

If you want to use environment variables other than those saved in your cypress.json (for example, in your circle or other ci tool) this is the solution that I used:

  1. Create an file wherever you store your cypress.json and cypress-retries.js files
  2. chmod the file so that it can be run
  3. paste the code snippet I shared below, replacing the "export" variables with your own. Official documentation can be found here:
  4. edit your config.yml file to run your file. Example in snippet 2.
// file

#! /bin/sh
export CYPRESS_baseUrl=$E2E_BASE_URL
node cypress-retries.js
//config.yml snippet

      - <<: *checkout_repo
      - run: echo "Starting end-to-end tests"
      - <<: *install_dependencies
      - run:
          name: Run end-to-end tests
          command: ./

This comment has been minimized.

Copy link

VSathyamoorthy commented Jan 11, 2019

I have added this script to my spec, could you please explain how do I execute a test suite from command line? the project using this below command, but got a respose

run command: node cypress-retries.js --record --key 07b5538b-87ec-4b24-945f-c51ca37cd837
response: We looked but did not find a cypress.json file in this folder: C:\xplanbase\apps\apex\9.99.999\cypress\integration\1_Cypress_retry

the cypress.json file is in this path C:\xplanbase\apps\apex\9.99.999

Thanks in advance your support

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.