Skip to content

Instantly share code, notes, and snippets.

@StormPooper

StormPooper/ci.js

Created Feb 11, 2019
Embed
What would you like to do?
Cypress CI script
/* eslint-disable no-console */
const argv = require('minimist')(process.argv.slice(2));
const cypress = require('cypress');
const chalk = require('chalk');
const fs = require('fs-extra');
const path = require('path');
const marge = require('mochawesome-report-generator');
cypress
.run(argv)
.then(results => {
console.log(
chalk.gray(
'────────────────────────────────────────────────────────────────────────────────────────────────────\n'
)
);
console.log(chalk.blue(` (${chalk.underline('Reports')})\n`));
const reportFolder = path.resolve(
results.config.parentTestsFolder,
'..',
results.config.reporterOptions.reportDir
);
const reports = fs
.readdirSync(reportFolder)
.filter(f => f.endsWith('.json'));
console.log(
chalk.gray(
` - Collecting reports:\t\t${chalk.white(
`${reports.length} found`
)}`
)
);
const specFiles = results.runs.map(run =>
path.relative(results.config.fileServerFolder, run.spec.name)
);
let reportData;
reports.forEach(report => {
console.log(
chalk.gray(` - Parsing report:\t\t${chalk.white(report)}`)
);
const json = fs.readJsonSync(path.resolve(reportFolder, report));
if (reportData == null) {
reportData = JSON.parse(JSON.stringify(json));
reportData.suites.suites = [];
} else {
[
'suites',
'tests',
'passing',
'pending',
'failures',
'duration',
'testsRegistered',
'passPercent',
'pendingPercent',
'other',
'skipped'
].forEach(key => {
reportData.stats[key] += json.stats[key];
});
}
const suites = JSON.parse(JSON.stringify(json.suites.suites));
const [first, ...rest] = suites;
const specFile = specFiles.find(f => f.endsWith(first.title));
const screenshotsFolder = path.resolve(
results.config.screenshotsFolder,
specFile
);
if (fs.existsSync(screenshotsFolder)) {
const screenshots = fs.readdirSync(screenshotsFolder);
screenshots.forEach(screenshot => {
const tree = path
.basename(screenshot, '.png')
.replace(' (failed)', '')
.split(' -- ');
let ref = rest;
tree.forEach(branch => {
ref = ref
? ref.find(
r =>
r.title.replace(
/[^0-9a-zA-Z-_\s\\)]/g,
''
) === branch
)
: undefined;
if (ref) {
if (ref.suites && ref.suites.length > 0)
ref = ref.suites;
else if (ref.tests && ref.tests.length > 0)
ref = ref.tests;
else
ref.context = JSON.stringify({
title: 'Screenshot',
value: `${path
.relative(
reportFolder,
path.resolve(
screenshotsFolder,
screenshot
)
)
.replace(/\\/g, '/')}`
});
}
});
});
}
first.tests[0].context = JSON.stringify({
title: 'Video',
value: `${path
.relative(
reportFolder,
path.resolve(
results.config.videosFolder,
`${specFile}.mp4`
)
)
.replace(/\\/g, '/')}`
});
first.suites = rest;
reportData.suites.suites.push(first);
});
reportData.stats.passPercent /= reports.length;
reportData.stats.pendingPercent /= reports.length;
if (reportData.stats.other > 0) reportData.stats.hasOther = true;
if (reportData.stats.skipped > 0) reportData.stats.hasSkipped = true;
[, results.config.reporterOptions.reportTitle] = argv.env.match(
/configFile=([^,]*)/i
);
console.log(
chalk.gray(
` - Started processing:\t\t${chalk.blue('Generating HTML')}`
)
);
marge.createSync(reportData, results.config.reporterOptions);
console.log(
chalk.gray(
` - Finished processing:\t${chalk.blue(
`${path.resolve(
reportFolder,
results.config.reporterOptions.reportFilename ||
'mochawesome'
)}.html`
)}`
)
);
console.log(
chalk.gray(
` - Starting cleanup:\t\t${chalk.white('Removing reports')}`
)
);
reports.forEach(report => {
fs.removeSync(path.resolve(reportFolder, report));
});
console.log(
chalk.gray(
` - Finished cleanup:\t\t${chalk.white(
`${reports.length} removed`
)}`
)
);
if (results.totalFailed > 0) process.exitCode = 1;
})
.catch(err => {
console.error(`\n ${chalk.red(`(${chalk.underline('Error')})`)}\n`);
console.error(err);
process.exitCode = 1;
})
.finally(() =>
console.log(
chalk.gray(
'\n====================================================================================================\n'
)
)
);
/* eslint-enable no-console */
@StormPooper

This comment has been minimized.

Copy link
Owner Author

@StormPooper StormPooper commented Dec 6, 2019

Linked to in my blog post, Adventures in End-to-End Testing with Cypress.

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.