Skip to content

Commit

Permalink
feat(cli): add flag to set exit code to the number of tests failed (#169
Browse files Browse the repository at this point in the history
)
  • Loading branch information
LironEr authored Nov 17, 2023
1 parent 241d067 commit d3f22a5
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 9 deletions.
12 changes: 11 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ const packageJson = require('./package.json');
.version(packageJson.version)
.option('-c, --config <path>', 'should be the same as "configOutput" reporter option', consts.defaultConfigOutput)
.option('-o, --output <path>', 'report output folder')
// TODO: change to true in the next major version?
.option('-e --set-exit-code', 'set the exit code to the number of failed tests', false)
.option('--debug', 'print debug logs', false);

program.parse();
const options = program.opts();

initDebugLog(options.debug === true);

debugLog(`cli options: ${JSON.stringify(options)}`)
debugLog(`cwd: ${process.cwd()}`);

log(`read config from ${options.config}`);
Expand All @@ -39,5 +42,12 @@ const packageJson = require('./package.json');
setSimpleConfig(config);

log('generate report');
await generateReport();
const report = await generateReport();

// replicate current cypress behavior
if (options.setExitCode && report.stats.failures > 0) {
debugLog(`${report.stats.failures} tests failed, set exit code`);

process.exit(report.stats.failures);
}
})();
6 changes: 4 additions & 2 deletions examples/cypress-parallel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,19 @@
1. Create report after test run:
```sh
npx generate-mochawesome-report
npx generate-mochawesome-report --set-exit-code
```
> `--set-exit-code` will set the exit code to the number of failed tests, as Cypress does. If you dont want this behavior, you can remove the flag or set `--set-exit-code false`.
Example scripts section in `package.json`:
```json
"scripts": {
"cy:run": "cypress run",
"cy:run:parallel": "cypress-parallel -s cy:run -t 2 -d 'cypress/e2e/**/*.cy.js' -r 'cypress-mochawesome-reporter' -o 'cypressParallel=true'",
"clean": "rimraf cypress/reports",
"generate-report": "generate-mochawesome-report",
"generate-report": "generate-mochawesome-report --set-exit-code",
"test": "npm run clean && npm run cy:run:parallel || true && npm run generate-report"
},
```
2 changes: 1 addition & 1 deletion examples/cypress-parallel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"cy:run": "cypress run",
"cy:run:parallel": "cypress-parallel -s cy:run -t 2 -d 'cypress/e2e/**/*.cy.js' -r 'cypress-mochawesome-reporter' -o 'cypressParallel=true'",
"clean": "rimraf cypress/reports",
"generate-report": "generate-mochawesome-report",
"generate-report": "generate-mochawesome-report --set-exit-code",
"test": "npm run clean && npm run cy:run:parallel || true && npm run generate-report"
},
"license": "MIT",
Expand Down
6 changes: 4 additions & 2 deletions lib/generateReport.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async function mergeAndCreate(jsonDir, screenshotsDir, mochawesomeOptions) {

debugLog(`HTML result: ${JSON.stringify(html)}`);

return html[0];
return { htmlPath: html[0], report };
}

async function copyMediaDir(inputDir, outputDir) {
Expand Down Expand Up @@ -61,14 +61,16 @@ async function generateReport() {
actions.push(copyMediaDir(videosFolder, path.join(outputDir, 'videos')));
}

const [htmlPath] = await Promise.all(actions);
const [{ htmlPath, report }] = await Promise.all(actions);

log('HTML report successfully created!');
log(htmlPath);

if (removeJsonsFolderAfterMerge) {
await fse.remove(jsonDir);
}

return report;
}

module.exports = generateReport;
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cypress-mochawesome-reporter",
"version": "3.6.1",
"version": "3.7.0",
"description": "Zero config Mochawesome reporter for Cypress with screenshots",
"engines": {
"node": ">=14"
Expand Down

0 comments on commit d3f22a5

Please sign in to comment.