Skip to content

Instantly share code, notes, and snippets.

@slinkardbrandon
Created September 17, 2021 21:02
Show Gist options
  • Save slinkardbrandon/afafc6438f8a539bb5b662785993d025 to your computer and use it in GitHub Desktop.
Save slinkardbrandon/afafc6438f8a539bb5b662785993d025 to your computer and use it in GitHub Desktop.
Cypress automatic globbing feature files and measurements
const cypress = require('cypress');
const { DateTime } = require('luxon');
const { green, white, yellow, red } = require('chalk');
const glob = require('glob');
const RUNTIME_MINUTES_GOOD = 6;
const RUNTIME_MINUTES_OKAY = 10;
const BASE_DIRECTORY = 'cypress/tests/integration';
/**
* Measure time it takes to run cypress tests (the native cypress timestamps
* do not account for browser launch time, page render time, etc).
*
* Also easily glob for feature files in your cypress test directory by passing
* any number of arguments to this node script, aside from "open"
*/
async function main() {
const args = process.argv.slice(2);
const before = DateTime.now();
const formattedStartTime = before.toFormat('tt');
let hasError = false;
const openArg = args.indexOf('open');
if (openArg !== -1) {
args.splice(openArg, 1);
}
const files = args
.map((file) => {
return `${BASE_DIRECTORY}/**/*/${file}.feature`;
})
.flatMap((f) => glob.sync(f, { realpath: true, nocase: true }));
try {
await cypress.run({
headless: openArg === -1,
browser: 'chrome',
record: false,
spec: files.join(','),
config: {
integrationFolder: '.',
baseUrl: 'http://localhost:5000',
video: false,
watchForFileChanges: false,
screenshotOnRunFailure: false,
},
});
hasError = false;
} catch (e) {
hasError = true;
console.error(e, '\n');
} finally {
const after = DateTime.now();
const formattedEndTime = after.toFormat('tt');
const minutes = Math.round((after.diff(before, ['minutes']).minutes + Number.EPSILON) * 100) / 100;
const color = minutes < RUNTIME_MINUTES_GOOD ? green : minutes < RUNTIME_MINUTES_OKAY ? yellow : red;
const outputTime = minutes > 1 ? `${minutes} minutes` : `${Math.round(minutes * 60 * 100) / 100} seconds`;
console.log(
white(
`\nRan from ${yellow(before.toFormat(formattedStartTime))} to ${yellow(after.toFormat(formattedEndTime))}.`,
`\nCompleted in ${color(outputTime)}.\n`
)
);
}
process.exit(hasError ? 1 : 0);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment