Skip to content

Commit

Permalink
(@wdio/json-reporter): add JSON reporter based of wdio-json-reporter (
Browse files Browse the repository at this point in the history
#11623)

* (@wdio/json-reporter): add JSON reporter based of wdio-json-reporter

* add reporter to package-lock.json

* trigger build

* fix fixture path
  • Loading branch information
christian-bromann committed Nov 8, 2023
1 parent be47a15 commit 5037108
Show file tree
Hide file tree
Showing 22 changed files with 509 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,5 @@ scripts/bidi/cddl
browserstack.err
e2e/tailwind.config.js
packages/webdriverio/.chrome
packages/wdio-json-reporter/tests/__fixtures__/merged.json
examples/wdio/mocha/*.json
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/wdio-cli/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ export const SUPPORTED_PACKAGES = {
{ name: 'allure', value: '@wdio/allure-reporter$--$allure' },
{ name: 'sumologic', value: '@wdio/sumologic-reporter$--$sumologic' },
{ name: 'concise', value: '@wdio/concise-reporter$--$concise' },
{ name: 'json', value: '@wdio/json-reporter$--$json' },
// external
{ name: 'reportportal', value: 'wdio-reportportal-reporter$--$reportportal' },
{ name: 'video', value: 'wdio-video-reporter$--$video' },
{ name: 'json', value: 'wdio-json-reporter$--$json' },
{ name: 'cucumber-json', value: 'wdio-cucumberjs-json-reporter$--$cucumberjs-json' },
{ name: 'mochawesome', value: 'wdio-mochawesome-reporter$--$mochawesome' },
{ name: 'timeline', value: 'wdio-timeline-reporter$--$timeline' },
Expand Down
4 changes: 4 additions & 0 deletions packages/wdio-json-reporter/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
src
tests
tsconfig.json
tsconfig.prod.json
20 changes: 20 additions & 0 deletions packages/wdio-json-reporter/LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) OpenJS Foundation and other contributors

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
76 changes: 76 additions & 0 deletions packages/wdio-json-reporter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# WDIO JSON Reporter

> A WebdriverIO plugin. Report results in json format.
## Installation

```bash
npm install @wdio/json-reporter --save-dev
```

## Configuration

### Results to `stdout`

```js
reporters: [
'dot',
['json', { stdout: true }]
],
```

### Results to File

```js
reporters: [
'dot',
['json',{
outputDir: './results'
}]
],
```

### Results to File with custom file name

```js
reporters: [
'dot',
['json',{
outputDir: './results',
outputFileFormat: (opts) => {
return `results-${opts.cid}.${opts.capabilities.browserName}.json`
}
}]
],
```

## Result Files

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.

`@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.

You can execute this in the [`onComplete`](https://webdriver.io/docs/configuration#oncomplete) of your `wdio.conf.js`:

```javascript
// wdio.conf.js
import mergeResults from '@wdio/json-reporter/mergeResults'

export const config = {
// ...
onComplete: function (exitCode, config, capabilities, results) {
mergeResults('./results', 'wdio-.*-json-reporter.json', 'wdio-custom-filename.json')
}
// ...
}
```

_Note:_ `wdio-custom-filename.json` is optional, is the parameter is not provided the default value is `wdio-merged.json`.

## Contribution

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!

---

For more information on WebdriverIO see the [homepage](http://webdriver.io).
45 changes: 45 additions & 0 deletions packages/wdio-json-reporter/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "@wdio/json-reporter",
"version": "8.21.0",
"description": "A WebdriverIO plugin to report results in json format.",
"author": "Christian Bromann <mail@bromann.dev>",
"homepage": "https://github.com/webdriverio/webdriverio/tree/main/packages/wdio-json-reporter",
"license": "MIT",
"engines": {
"node": "^16.13 || >=18"
},
"repository": {
"type": "git",
"url": "git://github.com/webdriverio/webdriverio.git",
"directory": "packages/wdio-json-reporter"
},
"keywords": [
"webdriver",
"wdio",
"wdio-reporter",
"json"
],
"bugs": {
"url": "https://github.com/webdriverio/webdriverio/issues"
},
"type": "module",
"exports": {
"./mergeResults": {
"types": "./build/mergeResults.d.ts",
"import": "./build/mergeResults.js",
"require": "./build/cjs/mergeResults.js"
},
".": "./build/index.js",
"./package.json": "./package.json"
},
"types": "./build/index.d.ts",
"typeScriptVersion": "3.8.3",
"dependencies": {
"@wdio/reporter": "8.21.0",
"@wdio/types": "8.21.0"
},
"devDependencies": {},
"publishConfig": {
"access": "public"
}
}
4 changes: 4 additions & 0 deletions packages/wdio-json-reporter/src/cjs/mergeResults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = async function (...args: any[]) {
const mergeResults = (await import('../mergeResults.js')).default
return mergeResults(...args)
}
3 changes: 3 additions & 0 deletions packages/wdio-json-reporter/src/cjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
61 changes: 61 additions & 0 deletions packages/wdio-json-reporter/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import WDIOReporter, { type RunnerStats } from '@wdio/reporter'
import type { Reporters } from '@wdio/types'

import type { ResultSet, TestSuite } from './types.js'

import { mapHooks, mapTests } from './utils.js'

export default class JsonReporter extends WDIOReporter {
constructor (options: Reporters.Options) {
if (options.logFile && options.logFile.endsWith('.log')) {
options.logFile = options.logFile.slice(0, -4) + '.json'
}
super(options)
}

onRunnerEnd (runner: RunnerStats) {
const json = this.#prepareJson(runner)
this.write(JSON.stringify(json))
}

#prepareJson (runner: RunnerStats) {
const resultSet: 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: 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
}
}
65 changes: 65 additions & 0 deletions packages/wdio-json-reporter/src/mergeResults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import type { ResultSet } from './types.js'

const DEFAULT_FILENAME = 'wdio-merged.json'

type MergedResultSet = Omit<ResultSet, 'capabilities'> & { capabilities: ResultSet['capabilities'][] }

export default async function mergeResults(
dir: string = process.argv[2],
filePattern: string | RegExp = process.argv[3],
customFileName: string = process.argv[4]
) {
const doesDirExist = fs.access(dir).then(() => true, () => false)
if (!doesDirExist) {
throw new Error(`Directory "${dir}" does not exist.`)
}
const rawData = await getDataFromFiles(dir, filePattern)
const mergedResults = mergeData(rawData)

if (customFileName) {
const fileName = customFileName || DEFAULT_FILENAME
const filePath = path.join(dir, fileName)
await fs.writeFile(filePath, JSON.stringify(mergedResults))
}

return mergedResults
}

async function getDataFromFiles (dir: string, filePattern: string | RegExp) {
const fileNames = (await fs.readdir(dir)).filter((file) => file.match(filePattern))
const data: any[] = []

await Promise.all(fileNames.map(async (fileName) => {
data.push(JSON.parse((await fs.readFile(`${dir}/${fileName}`)).toString()))
}))

return data as ResultSet[]
}

function mergeData (rawData: ResultSet[]) {
if (rawData.length === 0) {
return {} as MergedResultSet
}

const mergedResults: MergedResultSet = {
...rawData[0],
capabilities: [rawData[0].capabilities]
}

for (const data of rawData.slice(1)) {
mergedResults.suites.push(...data.suites)
mergedResults.specs.push(...data.specs)
mergedResults.state.passed += data.state.passed
mergedResults.state.failed += data.state.failed
mergedResults.state.skipped += data.state.skipped
mergedResults.capabilities.push(data.capabilities)
}

mergedResults.suites.forEach((suite) => {
mergedResults.end = (suite.end && mergedResults.end && suite.end > mergedResults.end ? suite.end : mergedResults.end)
})

return mergedResults
}
52 changes: 52 additions & 0 deletions packages/wdio-json-reporter/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type { Capabilities } from '@wdio/types'

export type State = 'passed' | 'failed' | 'skipped' | 'pending'

export interface SuiteState {
passed: number
failed: number
skipped: number
}

export interface ResultSet {
start: Date
end?: Date
capabilities: Capabilities.RemoteCapability
framework?: string
mochaOpts?: WebdriverIO.MochaOpts
jasmineOpts?: WebdriverIO.JasmineOpts
cucumberOpts?: WebdriverIO.CucumberOpts
suites: TestSuite[]
specs: string[]
state: SuiteState
}

export interface TestSuite {
name: string
duration: number
start: Date
end?: Date
sessionId?: string
tests: Test[]
hooks: Hook[]
}

export interface Test {
name: string
state: State
duration: number
start: Date
end?: Date
error?: Error
}

export interface Hook {
title: string
state: State
duration: number
start: Date
end?: Date
associatedSuite?: string
associatedTest?: string
error?: Error
}

0 comments on commit 5037108

Please sign in to comment.