Skip to content

Commit

Permalink
Add merge script (#40)
Browse files Browse the repository at this point in the history
* create script to merge files

* add documentation and change util export to make it more friendly to use

* version bump
  • Loading branch information
fijijavis authored May 29, 2019
1 parent ac7a70d commit 5031734
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1 deletion.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,36 @@ reporters: [
],
```

## Result Files
With WDIO v5, 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 we are no longer able
to get a single report for ALL test execution. Consider the following:

2 suites of tests configured to run in 2 browsers:

* WDIO v4: 1 json file with execution results
* WDIO v5: 4 json files with execution results


`wdio-mochawesome-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.

1) Create a small node script
```javascript
const mergeResults = require('wdio-mochawesome-reporter/mergeResults)
mergeResults()
```
2) Call node script from command line and pass 2 arguments
* <RESULTS_DIR>: Directory where results files are written
* <FILE_REGEX>: Regex pattern for finding `wdio-mochawesome-reporter` result files in <RESULTS_DIR>. This is necessary because multiple reporters produce `json` result files
Example:
```bash
node mergeResults.json ./Results "wdio-mochawesome-*"
```
Upon completion, the merge script will output a single json file named `wdio-ma-merged.json` in the provided <RESULTS_DIR>
# WDIO v4 Compatibility
Expand Down
2 changes: 2 additions & 0 deletions mergeResults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// make mergeResults available at the root
module.exports = require('./src/mergeResults')
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wdio-mochawesome-reporter",
"version": "3.0.1",
"version": "3.1.0",
"description": "A WebdriverIO plugin. Generates json results in Mochawesome format.",
"author": "Jim Davis <[email protected]>",
"homepage": "https://github.com/fijijavis/wdio-mochawesome-reporter.git#readme",
Expand All @@ -10,6 +10,10 @@
"url": "git+https://github.com/fijijavis/wdio-mochawesome-reporter.git"
},
"main": "src/index.js",
"files": [
"src",
"mergeResults.js"
],
"scripts": {
"lint": "eslint ./src test/",
"test": "jest"
Expand Down
60 changes: 60 additions & 0 deletions src/mergeResults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const fs = require('fs')
const path = require('path')

const mergeResults = (...args) => {
const dir = process.argv[2]
const filePattern = process.argv[3]

const rawData = getDataFromFiles(dir, filePattern)
const mergedResults = mergeData(rawData)
writeFile(dir, mergedResults)
}

function getDataFromFiles (dir, filePattern) {
const fileNames = fs.readdirSync(dir).filter(file => file.match(filePattern))
const data = []

fileNames.forEach(fileName => {
data.push(JSON.parse(fs.readFileSync(`${dir}/${fileName}`)))
})

return data
}

function mergeData (rawData) {
let mergedResults
rawData.forEach(data => {
if (mergedResults === undefined) {
// use the first result so that we have the right shape for the mochawesome report generator
mergedResults = {}
Object.assign(mergedResults, data)
} else {
// increment stats totals
mergedResults.stats.suites += data.stats.suites
mergedResults.stats.tests += data.stats.tests
mergedResults.stats.passes += data.stats.passes
mergedResults.stats.pending += data.stats.pending
mergedResults.stats.failures += data.stats.failures
mergedResults.stats.duration += data.stats.duration
mergedResults.stats.testsRegistered += data.stats.testsRegistered
mergedResults.stats.skipped += data.stats.skipped
mergedResults.stats.hasSkipped = mergedResults.stats.skipped > 0
mergedResults.stats.passPercent = mergedResults.stats.tests === 0 ? 0 : Math.round((mergedResults.stats.passes / mergedResults.stats.tests) * 100)
mergedResults.stats.pendingPercent = mergedResults.stats.tests === 0 ? 0 : Math.round((mergedResults.stats.pending / mergedResults.stats.tests) * 100)

// add suites
data.suites.suites.forEach(suite => {
mergedResults.suites.suites.push(suite)
})
}
})
return mergedResults
}

function writeFile (dir, mergedResults) {
const fileName = 'wdio-ma-merged.json'
const filePath = path.join(dir, fileName)
fs.writeFileSync(filePath, JSON.stringify(mergedResults))
}

module.exports = mergeResults

0 comments on commit 5031734

Please sign in to comment.