Skip to content

Commit

Permalink
chore: fix Cypress summary report
Browse files Browse the repository at this point in the history
  • Loading branch information
mistic100 committed Oct 27, 2024
1 parent f61865e commit cf9b9aa
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 99 deletions.
16 changes: 9 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ jobs:
run: yarn ci:build

- name: test summary
uses: dorny/test-reporter@1a288b62f8b75c0f433cbfdbc2e4800fbae50bd7
uses: phoenix-actions/test-reporting@v15
if: ${{ (success() || failure()) && github.repository == 'mistic100/Photo-Sphere-Viewer' }}
with:
name: build
use-actions-summary: true
output-to: step-summary
path: 'packages/*/reports/mocha.json'
reporter: mocha-json

Expand All @@ -55,16 +55,18 @@ jobs:
if: ${{ (success() || failure()) && github.repository == 'mistic100/Photo-Sphere-Viewer' }}
with:
name: e2e-report-${{ github.ref_name }}-${{ env.SHORT_SHA }}
path: cypress/reports/html
path: |
cypress/reports/html
!cypress/reports/html/.jsons
- name: test summary
uses: dorny/test-reporter@1a288b62f8b75c0f433cbfdbc2e4800fbae50bd7
uses: phoenix-actions/test-reporting@v15
if: ${{ (success() || failure()) && github.repository == 'mistic100/Photo-Sphere-Viewer' }}
with:
name: e2e
use-actions-summary: true
path: cypress/reports/e2e.json
reporter: mocha-json
output-to: step-summary
path: 'cypress/reports/html/.jsons/*.json'
reporter: mochawesome-json

build-doc:
runs-on: ubuntu-latest
Expand Down
92 changes: 0 additions & 92 deletions build/mocha-reporter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const BaseReporter = require('mocha/lib/reporters/base');
const SpecReporter = require('mocha/lib/reporters/spec');
const path = require('path');
const fs = require('fs');

module.exports = class MultiReporter extends BaseReporter {
constructor(runner, options) {
Expand All @@ -12,12 +10,6 @@ module.exports = class MultiReporter extends BaseReporter {
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, {
Expand All @@ -36,87 +28,3 @@ module.exports = class MultiReporter extends BaseReporter {
}
}
};

// custom json reporter for cypress (for some reason, calling JsonReporter does not work)
// - write the results to a json file
// - fill the "file" property of each test
function CypressJsonReporter(runner, options) {
BaseReporter.call(this, runner, options);

const tests = [];
const pending = [];
const failures = [];
const passes = [];

runner.on('test end', function (test) {
tests.push(test);
});

runner.on('pass', function (test) {
passes.push(test);
});

runner.on('fail', function (test) {
failures.push(test);
});

runner.on('pending', function (test) {
pending.push(test);
});

runner.once('end', () => {
const obj = {
stats: this.stats,
tests: tests.map(clean),
pending: pending.map(clean),
failures: failures.map(clean),
passes: passes.map(clean),
};
const json = JSON.stringify(obj, null, 2);
const output = options.reporterOption.output;
fs.mkdirSync(path.dirname(output), { recursive: true });
fs.writeFileSync(output, json);
});

function clean(test) {
let err = test.err || {};
if (err instanceof Error) {
err = errorJSON(err);
}

return {
title: test.title,
fullTitle: test.fullTitle(),
file: test.invocationDetails.absoluteFile,
duration: test.duration,
currentRetry: test.currentRetry(),
speed: test.speed,
err: cleanCycles(err),
};
}

function cleanCycles(obj) {
let cache = [];
return JSON.parse(
JSON.stringify(obj, (key, value) => {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
// Instead of going in a circle, we'll print [object Object]
return '' + value;
}
cache.push(value);
}

return value;
})
);
}

function errorJSON(err) {
let res = {};
Object.getOwnPropertyNames(err).forEach((key) => {
res[key] = err[key];
}, err);
return res;
}
}
1 change: 1 addition & 0 deletions cypress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ export default defineConfig({
reporter: 'build/mocha-reporter.js',
reporterOptions: {
cypress: true,
removeJsonsFolderAfterMerge: false, // cypress-mochawesome-reporter
},
});

0 comments on commit cf9b9aa

Please sign in to comment.