Skip to content

Commit

Permalink
chore: Cypress HTML report
Browse files Browse the repository at this point in the history
  • Loading branch information
mistic100 committed Oct 24, 2024
1 parent b2d8904 commit a2f9196
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 8 deletions.
13 changes: 12 additions & 1 deletion build/mocha-reporter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const BaseReporter = require('mocha/lib/reporters/base');
const SpecReporter = require('mocha/lib/reporters/spec');
const JsonReporter = require('mocha/lib/reporters/json');
const path = require('path');
const fs = require('fs');

Expand All @@ -11,19 +10,31 @@ module.exports = class MultiReporter extends BaseReporter {
new SpecReporter(runner, {});

if (options.reporterOptions?.cypress) {
const MochawesomeReporter = require('cypress-mochawesome-reporter/lib/reporter');
this.mochawesome = new MochawesomeReporter(runner, {});

new CypressJsonReporter(runner, {
reporterOption: {
output: 'cypress/reports/e2e.json',
},
});
} else {
const JsonReporter = require('mocha/lib/reporters/json');
new JsonReporter(runner, {
reporterOption: {
output: 'reports/mocha.json',
},
});
}
}

done(failures, exit) {
if (this.mochawesome) {
this.mochawesome.done(failures, exit);
} else {
exit(failures);
}
}
};

// custom json reporter for cypress (for some reason, calling JsonReporter does not work)
Expand Down
3 changes: 3 additions & 0 deletions cypress.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { defineConfig } from 'cypress';
import { configureVisualRegression } from 'cypress-visual-regression';
// @ts-ignore
import cypressMochawesomeReporterPlugin from 'cypress-mochawesome-reporter/plugin';

export default defineConfig({
e2e: {
Expand All @@ -13,6 +15,7 @@ export default defineConfig({
},
setupNodeEvents(on) {
configureVisualRegression(on);
cypressMochawesomeReporterPlugin(on);

on('before:browser:launch', (browser, launchOptions) => {
// should be bigger than the largest viewport used + browser UI elements
Expand Down
21 changes: 21 additions & 0 deletions cypress/support/e2e.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import 'cypress-real-events';
import 'cypress-mochawesome-reporter/register';
import { addCompareSnapshotCommand } from 'cypress-visual-regression/dist/command';

addCompareSnapshotCommand({
errorThreshold: 0.1,
failSilently: !Cypress.config('isInteractive'),
});

if (!Cypress.config('isInteractive')) {
Cypress.Commands.overwrite('compareSnapshot', (originalFn, name, options = {}) => {
return originalFn(name, options)
.then(result => {
if (result.images.diff) {
// @ts-ignore
Cypress.Mochawesome.context.push({ title: 'Visual regression diff', value: 'data:image/png;base64,' + result.images.diff });
}

if (result.error) {
throw new Error(result.error);
}

return result;
});
});
}
3 changes: 2 additions & 1 deletion cypress/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"types": [
"cypress",
"node",
"cypress-real-events"
"cypress-real-events",
"cypress-mochawesome-reporter"
]
}
}
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@typescript-eslint/parser": "^7.16.0",
"alive-server": "^1.3.0",
"cypress": "^13.15.0",
"cypress-mochawesome-reporter": "^3.8.2",
"cypress-real-events": "^1.13.0",
"cypress-visual-regression": "^5.2.2",
"esbuild-sass-plugin": "^3.3.0",
Expand All @@ -59,5 +60,8 @@
"typedoc": "^0.26.7",
"typedoc-plugin-extras": "^3.1.0",
"typescript": "^5.6.2"
},
"resolutions": {
"yargs": "^16.2.0"
}
}
Loading

0 comments on commit a2f9196

Please sign in to comment.