@wdio/json-reporter
Version:
A WebdriverIO plugin to report results in json format.
85 lines (82 loc) • 2.34 kB
JavaScript
// src/index.ts
import WDIOReporter from "@wdio/reporter";
// src/utils.ts
function mapHooks(hooks) {
return hooks.map((hook) => ({
start: hook.start,
end: hook.end,
duration: hook.duration,
title: hook.title,
associatedSuite: hook.parent,
associatedTest: hook.currentTest,
state: hook.errors && hook.errors.length && hook.state ? hook.state : "passed",
error: hook.error
}));
}
function mapTests(tests) {
return tests.map((test) => ({
name: test.title,
start: test.start,
end: test.end,
duration: test.duration,
state: test.state,
error: test.error
}));
}
// src/index.ts
var JsonReporter = class extends WDIOReporter {
constructor(options) {
if (options.logFile && options.logFile.endsWith(".log")) {
options.logFile = options.logFile.slice(0, -4) + ".json";
}
super(options);
}
onRunnerEnd(runner) {
const json = this.#prepareJson(runner);
this.write(JSON.stringify(json));
}
#prepareJson(runner) {
const resultSet = {
start: runner.start,
end: runner.end,
capabilities: runner.capabilities,
framework: runner.config.framework,
mochaOpts: runner.config.mochaOpts,
suites: [],
specs: [],
state: { passed: 0, failed: 0, skipped: 0 }
};
for (const spec of runner.specs) {
resultSet.specs.push(spec);
for (const suiteKey of Object.keys(this.suites)) {
const suite = this.suites[suiteKey];
const testSuite = {
name: suite.title,
duration: suite._duration,
start: suite.start,
end: suite.end,
sessionId: runner.sessionId,
tests: mapTests(suite.tests),
hooks: mapHooks(suite.hooks)
};
resultSet.state.failed += testSuite.hooks.filter(
(hook) => hook.error
).length;
resultSet.state.passed += testSuite.tests.filter(
(test) => test.state === "passed"
).length;
resultSet.state.failed += testSuite.tests.filter(
(test) => test.state === "failed"
).length;
resultSet.state.skipped += testSuite.tests.filter(
(test) => test.state === "skipped"
).length;
resultSet.suites.push(testSuite);
}
}
return resultSet;
}
};
export {
JsonReporter as default
};