Skip to content

Commit

Permalink
fix json reports
Browse files Browse the repository at this point in the history
  • Loading branch information
mistic100 committed Oct 18, 2024
1 parent 3e4348e commit 6e25ac0
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 15 deletions.
110 changes: 99 additions & 11 deletions build/mocha-reporter.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,111 @@
const BaseReporter = require('mocha/lib/reporters/base');
const SpecReporter = require('mocha/lib/reporters/spec');
// Cypress breaks the reporter ?
// https://github.com/cypress-io/cypress/issues/27636
const JsonReporter = require('../node_modules/mocha/lib/reporters/json.js');
const JsonReporter = require('mocha/lib/reporters/json');
const path = require('path');
const fs = require('fs');

module.exports = class MultiReporter extends BaseReporter {
constructor(runner, options) {
super(runner, options);

// mocha uses "reporterOption", cypress uses "reporterOptions"
const title = options.reporterOption?.title ?? options.reporterOptions?.title ?? 'report';

new SpecReporter(runner, {});

new JsonReporter(runner, {
reporterOption: {
output: path.join(__dirname, '../reports', `${title}.json`),
},
});
if (options.reporterOptions?.cypress) {
new CypressJsonReporter(runner, {
reporterOption: {
output: path.join(__dirname, '../reports/e2e.json'),
},
});
} else {
new JsonReporter(runner, {
reporterOption: {
output: path.join(__dirname, '../reports', `${options.reporterOption.title}.json`),
},
});
}
}
};

// 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;
}
}
2 changes: 1 addition & 1 deletion cypress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ export default defineConfig({
},
reporter: 'build/mocha-reporter.js',
reporterOptions: {
title: 'e2e',
cypress: true,
},
});
2 changes: 1 addition & 1 deletion packages/core/src/utils/math.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from 'assert';
import { greatArcDistance } from './math';

describe('utils:math:greatArcDistance', () => {
it('', () => {
it('should compute the great-arc distance', () => {
// easy
assert.strictEqual(greatArcDistance([0, 0], [Math.PI, 0]), Math.PI);
assert.strictEqual(greatArcDistance([Math.PI / 2, 0], [(3 * Math.PI) / 2, 0]), Math.PI);
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/tests/generate-typedoc-readme.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import assert from 'assert';
const testDir = path.join(__dirname, 'fixtures/generate-typedoc-readme');

describe('generate-typedoc-readme', () => {
it('should works', () => {
it('should generate the readme', () => {
execSync(`node ${path.join(__dirname, '../../../build/generate-typedoc-readme.mjs')}`, { cwd: testDir });

const cases = {
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/tests/prepare-changelog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import assert from 'assert';
const testDir = path.join(__dirname, 'fixtures/prepare-changelog');

describe('prepare-changelog', () => {
it('should works', (done) => {
it('should generate the changelog', (done) => {
const gitLog = readFileSync(path.join(testDir, 'git-log.txt'), { encoding: 'utf8' });

const proc = exec(`node ${path.join(__dirname, '../../../build/prepare-changelog.mjs')} 5.7.4 5.8.0`, { cwd: testDir }, (err) => {
Expand Down

0 comments on commit 6e25ac0

Please sign in to comment.