@wdio/json-reporter 8.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE-MIT ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) OpenJS Foundation and other contributors
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ 'Software'), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # WDIO JSON Reporter
2
+
3
+ > A WebdriverIO plugin. Report results in json format.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @wdio/json-reporter --save-dev
9
+ ```
10
+
11
+ ## Configuration
12
+
13
+ ### Results to `stdout`
14
+
15
+ ```js
16
+ reporters: [
17
+ 'dot',
18
+ ['json', { stdout: true }]
19
+ ],
20
+ ```
21
+
22
+ ### Results to File
23
+
24
+ ```js
25
+ reporters: [
26
+ 'dot',
27
+ ['json',{
28
+ outputDir: './results'
29
+ }]
30
+ ],
31
+ ```
32
+
33
+ ### Results to File with custom file name
34
+
35
+ ```js
36
+ reporters: [
37
+ 'dot',
38
+ ['json',{
39
+ outputDir: './results',
40
+ outputFileFormat: (opts) => {
41
+ return `results-${opts.cid}.${opts.capabilities.browserName}.json`
42
+ }
43
+ }]
44
+ ],
45
+ ```
46
+
47
+ ## Result Files
48
+
49
+ With WDIO v5 upwards, reporting has moved from a centralized process to one that is handled by each of the "sessions" spun up for parallel test execution. This change helped reduce the amount of chatter during WDIO test execution and thus improved performance. The downside is it is no longer possible to get a single report for all test execution.
50
+
51
+ `@wdio/json-reporter` provides a utility function to merge the multiple json files into a single file. Follow the steps below to take advantage of the utility.
52
+
53
+ You can execute this in the [`onComplete`](https://webdriver.io/docs/configuration#oncomplete) of your `wdio.conf.js`:
54
+
55
+ ```javascript
56
+ // wdio.conf.js
57
+ import mergeResults from '@wdio/json-reporter/mergeResults'
58
+
59
+ export const config = {
60
+ // ...
61
+ onComplete: function (exitCode, config, capabilities, results) {
62
+ mergeResults('./results', 'wdio-.*-json-reporter.json', 'wdio-custom-filename.json')
63
+ }
64
+ // ...
65
+ }
66
+ ```
67
+
68
+ _Note:_ `wdio-custom-filename.json` is optional, is the parameter is not provided the default value is `wdio-merged.json`.
69
+
70
+ ## Contribution
71
+
72
+ The source code of this reporter was highly inspired by the [`wdio-json-reporter`](https://github.com/fijijavis/wdio-json-reporter) community reporter by [Jim Davis](https://github.com/fijijavis). Thanks for all the work maintaining the project!
73
+
74
+ ---
75
+
76
+ For more information on WebdriverIO see the [homepage](http://webdriver.io).
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=mergeResults.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mergeResults.d.ts","sourceRoot":"","sources":["../../src/cjs/mergeResults.ts"],"names":[],"mappings":""}
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ module.exports = async function (...args) {
4
+ const mergeResults = (await import('../mergeResults.js')).default;
5
+ return mergeResults(...args);
6
+ };
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,8 @@
1
+ import WDIOReporter, { type RunnerStats } from '@wdio/reporter';
2
+ import type { Reporters } from '@wdio/types';
3
+ export default class JsonReporter extends WDIOReporter {
4
+ #private;
5
+ constructor(options: Reporters.Options);
6
+ onRunnerEnd(runner: RunnerStats): void;
7
+ }
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,EAAE,EAAE,KAAK,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAC/D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAM5C,MAAM,CAAC,OAAO,OAAO,YAAa,SAAQ,YAAY;;gBACrC,OAAO,EAAE,SAAS,CAAC,OAAO;IAOvC,WAAW,CAAE,MAAM,EAAE,WAAW;CA6CnC"}
package/build/index.js ADDED
@@ -0,0 +1,47 @@
1
+ import WDIOReporter from '@wdio/reporter';
2
+ import { mapHooks, mapTests } from './utils.js';
3
+ export default class JsonReporter extends WDIOReporter {
4
+ constructor(options) {
5
+ if (options.logFile && options.logFile.endsWith('.log')) {
6
+ options.logFile = options.logFile.slice(0, -4) + '.json';
7
+ }
8
+ super(options);
9
+ }
10
+ onRunnerEnd(runner) {
11
+ const json = this.#prepareJson(runner);
12
+ this.write(JSON.stringify(json));
13
+ }
14
+ #prepareJson(runner) {
15
+ const resultSet = {
16
+ start: runner.start,
17
+ end: runner.end,
18
+ capabilities: runner.capabilities,
19
+ framework: runner.config.framework,
20
+ mochaOpts: runner.config.mochaOpts,
21
+ suites: [],
22
+ specs: [],
23
+ state: { passed: 0, failed: 0, skipped: 0 },
24
+ };
25
+ for (const spec of runner.specs) {
26
+ resultSet.specs.push(spec);
27
+ for (const suiteKey of Object.keys(this.suites)) {
28
+ const suite = this.suites[suiteKey];
29
+ const testSuite = {
30
+ name: suite.title,
31
+ duration: suite._duration,
32
+ start: suite.start,
33
+ end: suite.end,
34
+ sessionId: runner.sessionId,
35
+ tests: mapTests(suite.tests),
36
+ hooks: mapHooks(suite.hooks)
37
+ };
38
+ resultSet.state.failed += testSuite.hooks.filter((hook) => hook.error).length;
39
+ resultSet.state.passed += testSuite.tests.filter((test) => test.state === 'passed').length;
40
+ resultSet.state.failed += testSuite.tests.filter((test) => test.state === 'failed').length;
41
+ resultSet.state.skipped += testSuite.tests.filter((test) => test.state === 'skipped').length;
42
+ resultSet.suites.push(testSuite);
43
+ }
44
+ }
45
+ return resultSet;
46
+ }
47
+ }
@@ -0,0 +1,7 @@
1
+ import type { ResultSet } from './types.js';
2
+ type MergedResultSet = Omit<ResultSet, 'capabilities'> & {
3
+ capabilities: ResultSet['capabilities'][];
4
+ };
5
+ export default function mergeResults(dir?: string, filePattern?: string | RegExp, customFileName?: string): Promise<MergedResultSet>;
6
+ export {};
7
+ //# sourceMappingURL=mergeResults.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mergeResults.d.ts","sourceRoot":"","sources":["../src/mergeResults.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAI3C,KAAK,eAAe,GAAG,IAAI,CAAC,SAAS,EAAE,cAAc,CAAC,GAAG;IAAE,YAAY,EAAE,SAAS,CAAC,cAAc,CAAC,EAAE,CAAA;CAAE,CAAA;AAEtG,wBAA8B,YAAY,CACtC,GAAG,GAAE,MAAwB,EAC7B,WAAW,GAAE,MAAM,GAAG,MAAwB,EAC9C,cAAc,GAAE,MAAwB,4BAgB3C"}
@@ -0,0 +1,46 @@
1
+ import fs from 'node:fs/promises';
2
+ import path from 'node:path';
3
+ const DEFAULT_FILENAME = 'wdio-merged.json';
4
+ export default async function mergeResults(dir = process.argv[2], filePattern = process.argv[3], customFileName = process.argv[4]) {
5
+ const doesDirExist = fs.access(dir).then(() => true, () => false);
6
+ if (!doesDirExist) {
7
+ throw new Error(`Directory "${dir}" does not exist.`);
8
+ }
9
+ const rawData = await getDataFromFiles(dir, filePattern);
10
+ const mergedResults = mergeData(rawData);
11
+ if (customFileName) {
12
+ const fileName = customFileName || DEFAULT_FILENAME;
13
+ const filePath = path.join(dir, fileName);
14
+ await fs.writeFile(filePath, JSON.stringify(mergedResults));
15
+ }
16
+ return mergedResults;
17
+ }
18
+ async function getDataFromFiles(dir, filePattern) {
19
+ const fileNames = (await fs.readdir(dir)).filter((file) => file.match(filePattern));
20
+ const data = [];
21
+ await Promise.all(fileNames.map(async (fileName) => {
22
+ data.push(JSON.parse((await fs.readFile(`${dir}/${fileName}`)).toString()));
23
+ }));
24
+ return data;
25
+ }
26
+ function mergeData(rawData) {
27
+ if (rawData.length === 0) {
28
+ return {};
29
+ }
30
+ const mergedResults = {
31
+ ...rawData[0],
32
+ capabilities: [rawData[0].capabilities]
33
+ };
34
+ for (const data of rawData.slice(1)) {
35
+ mergedResults.suites.push(...data.suites);
36
+ mergedResults.specs.push(...data.specs);
37
+ mergedResults.state.passed += data.state.passed;
38
+ mergedResults.state.failed += data.state.failed;
39
+ mergedResults.state.skipped += data.state.skipped;
40
+ mergedResults.capabilities.push(data.capabilities);
41
+ }
42
+ mergedResults.suites.forEach((suite) => {
43
+ mergedResults.end = (suite.end && mergedResults.end && suite.end > mergedResults.end ? suite.end : mergedResults.end);
44
+ });
45
+ return mergedResults;
46
+ }
@@ -0,0 +1,47 @@
1
+ import type { Capabilities } from '@wdio/types';
2
+ export type State = 'passed' | 'failed' | 'skipped' | 'pending';
3
+ export interface SuiteState {
4
+ passed: number;
5
+ failed: number;
6
+ skipped: number;
7
+ }
8
+ export interface ResultSet {
9
+ start: Date;
10
+ end?: Date;
11
+ capabilities: Capabilities.RemoteCapability;
12
+ framework?: string;
13
+ mochaOpts?: WebdriverIO.MochaOpts;
14
+ jasmineOpts?: WebdriverIO.JasmineOpts;
15
+ cucumberOpts?: WebdriverIO.CucumberOpts;
16
+ suites: TestSuite[];
17
+ specs: string[];
18
+ state: SuiteState;
19
+ }
20
+ export interface TestSuite {
21
+ name: string;
22
+ duration: number;
23
+ start: Date;
24
+ end?: Date;
25
+ sessionId?: string;
26
+ tests: Test[];
27
+ hooks: Hook[];
28
+ }
29
+ export interface Test {
30
+ name: string;
31
+ state: State;
32
+ duration: number;
33
+ start: Date;
34
+ end?: Date;
35
+ error?: Error;
36
+ }
37
+ export interface Hook {
38
+ title: string;
39
+ state: State;
40
+ duration: number;
41
+ start: Date;
42
+ end?: Date;
43
+ associatedSuite?: string;
44
+ associatedTest?: string;
45
+ error?: Error;
46
+ }
47
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAE/C,MAAM,MAAM,KAAK,GAAG,QAAQ,GAAG,QAAQ,GAAG,SAAS,GAAG,SAAS,CAAA;AAE/D,MAAM,WAAW,UAAU;IACvB,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,MAAM,CAAA;CAClB;AAED,MAAM,WAAW,SAAS;IACtB,KAAK,EAAE,IAAI,CAAA;IACX,GAAG,CAAC,EAAE,IAAI,CAAA;IACV,YAAY,EAAE,YAAY,CAAC,gBAAgB,CAAA;IAC3C,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,SAAS,CAAC,EAAE,WAAW,CAAC,SAAS,CAAA;IACjC,WAAW,CAAC,EAAE,WAAW,CAAC,WAAW,CAAA;IACrC,YAAY,CAAC,EAAE,WAAW,CAAC,YAAY,CAAA;IACvC,MAAM,EAAE,SAAS,EAAE,CAAA;IACnB,KAAK,EAAE,MAAM,EAAE,CAAA;IACf,KAAK,EAAE,UAAU,CAAA;CACpB;AAED,MAAM,WAAW,SAAS;IACtB,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,MAAM,CAAA;IAChB,KAAK,EAAE,IAAI,CAAA;IACX,GAAG,CAAC,EAAE,IAAI,CAAA;IACV,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,KAAK,EAAE,IAAI,EAAE,CAAA;IACb,KAAK,EAAE,IAAI,EAAE,CAAA;CAChB;AAED,MAAM,WAAW,IAAI;IACjB,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,KAAK,CAAA;IACZ,QAAQ,EAAE,MAAM,CAAA;IAChB,KAAK,EAAE,IAAI,CAAA;IACX,GAAG,CAAC,EAAE,IAAI,CAAA;IACV,KAAK,CAAC,EAAE,KAAK,CAAA;CAChB;AAED,MAAM,WAAW,IAAI;IACjB,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,EAAE,KAAK,CAAA;IACZ,QAAQ,EAAE,MAAM,CAAA;IAChB,KAAK,EAAE,IAAI,CAAA;IACX,GAAG,CAAC,EAAE,IAAI,CAAA;IACV,eAAe,CAAC,EAAE,MAAM,CAAA;IACxB,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,KAAK,CAAC,EAAE,KAAK,CAAA;CAChB"}
package/build/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ import type { HookStats, TestStats } from '@wdio/reporter';
2
+ import type { Hook, Test } from './types.js';
3
+ export declare function mapHooks(hooks: HookStats[]): Hook[];
4
+ export declare function mapTests(tests: TestStats[]): Test[];
5
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1D,OAAO,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,YAAY,CAAA;AAE5C,wBAAgB,QAAQ,CAAE,KAAK,EAAE,SAAS,EAAE,GAAG,IAAI,EAAE,CAWpD;AAED,wBAAgB,QAAQ,CAAE,KAAK,EAAE,SAAS,EAAE,GAAG,IAAI,EAAE,CASpD"}
package/build/utils.js ADDED
@@ -0,0 +1,22 @@
1
+ export function mapHooks(hooks) {
2
+ return hooks.map((hook) => ({
3
+ start: hook.start,
4
+ end: hook.end,
5
+ duration: hook.duration,
6
+ title: hook.title,
7
+ associatedSuite: hook.parent,
8
+ associatedTest: hook.currentTest,
9
+ state: hook.errors && hook.errors.length && hook.state ? hook.state : 'passed',
10
+ error: hook.error
11
+ }));
12
+ }
13
+ export function mapTests(tests) {
14
+ return tests.map((test) => ({
15
+ name: test.title,
16
+ start: test.start,
17
+ end: test.end,
18
+ duration: test.duration,
19
+ state: test.state,
20
+ error: test.error
21
+ }));
22
+ }
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@wdio/json-reporter",
3
+ "version": "8.22.0",
4
+ "description": "A WebdriverIO plugin to report results in json format.",
5
+ "author": "Christian Bromann <mail@bromann.dev>",
6
+ "homepage": "https://github.com/webdriverio/webdriverio/tree/main/packages/wdio-json-reporter",
7
+ "license": "MIT",
8
+ "engines": {
9
+ "node": "^16.13 || >=18"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git://github.com/webdriverio/webdriverio.git",
14
+ "directory": "packages/wdio-json-reporter"
15
+ },
16
+ "keywords": [
17
+ "webdriver",
18
+ "wdio",
19
+ "wdio-reporter",
20
+ "json"
21
+ ],
22
+ "bugs": {
23
+ "url": "https://github.com/webdriverio/webdriverio/issues"
24
+ },
25
+ "type": "module",
26
+ "exports": {
27
+ "./mergeResults": {
28
+ "types": "./build/mergeResults.d.ts",
29
+ "import": "./build/mergeResults.js",
30
+ "require": "./build/cjs/mergeResults.js"
31
+ },
32
+ ".": "./build/index.js",
33
+ "./package.json": "./package.json"
34
+ },
35
+ "types": "./build/index.d.ts",
36
+ "typeScriptVersion": "3.8.3",
37
+ "dependencies": {
38
+ "@wdio/reporter": "8.21.0",
39
+ "@wdio/types": "8.21.0"
40
+ },
41
+ "publishConfig": {
42
+ "access": "public"
43
+ },
44
+ "gitHead": "81496a30b1c50e7d4129be6997f1a557f9659224"
45
+ }