Skip to content

Instantly share code, notes, and snippets.

@edelgado
Created October 14, 2019 01:51
Show Gist options
  • Save edelgado/9dbceac4e275c0579fe91170ecc35645 to your computer and use it in GitHub Desktop.
Save edelgado/9dbceac4e275c0579fe91170ecc35645 to your computer and use it in GitHub Desktop.
Safely Run Cypress Tests
/*
The free Cypress tier which limits run records to 500.
Once the limit is reached, it does not run the tests and exists with an error code.
This makes CI tests fail, and the PR checks to fail as result. We do not want that
to happen, so we are using this wrapper run script to attempt to run the tests with
recordings on. If there is an error (eg the limit has been reached), we will attempt
the run again, this time without recording.
Source: https://github.com/cypress-io/cypress/issues/4021
*/
const cypress = require("cypress");
const { argv } = require("yargs");
const didRunError = results =>
results.message &&
results.message.includes("Could not find Cypress test run results");
const options = {};
if (argv.browser || argv.b) options.browser = argv.browser || argv.b;
if (argv["ci-build-id"]) options.ciBuildId = argv["ci-build-id"];
if (argv.config || argv.c) options.config = argv.config || argv.c;
if (argv.env || argv.e) options.env = argv.env || argv.e;
if (argv.group) options.group = argv.group;
if (argv.headed) options.headed = argv.headed;
if (argv.help || argv.h) options.help = argv.help || argv.h;
if (argv.key || argv.k) options.key = argv.key || argv.k;
if (argv["no-exit"]) options.noExit = true;
if (argv.parallel) options.parallel = true;
if (argv.port || argv.p) options.port = argv.port || argv.p;
if (argv.project || argv.P) options.project = argv.project || argv.P;
if (argv.reporter || argv.r) options.reporter = argv.reporter || argv.r;
if (argv["reporter-options"] || argv.o)
options.reporterOptions = argv["reporter-options"] || argv.o;
if (argv.spec || argv.s) options.spec = argv.spec || argv.s;
// Run using the Cypress module API. The record key should be available in the
// CYPRESS_RECORD_KEY env var when this runs in CI.
cypress
.run({
record: true,
...options
})
.then((results = {}) => {
if (didRunError(results)) {
cypress
.run({
record: false,
...options
})
.then((results2 = {}) => {
process.exit(results2.totalFailed);
})
.catch(err => {
console.log("==> Error trying to run the tests:", err);
process.exit(1);
});
} else {
process.exit(results.totalFailed);
}
return null;
})
.catch(err => {
console.log("==> Error trying to run the tests:", err);
process.exit(1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment