forked from JeremyHeleine/Photo-Sphere-Viewer
-
-
Notifications
You must be signed in to change notification settings - Fork 686
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
103 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters