How to merge Junit XML report in Cypress to integrate with AWS CB

7k views Asked by At

I have initially used Mochaawesome report but cannot integrate with AWS. It turned out I need JUnit XML reporter in order to integrate with code build.

I've created Junit XML report but I don't know how to merge them into one xml file so that it can be used in AWS.

XML files got created (which I've been trying to merge them)

enter image description here

Cypress.json file

"reporter": "cypress-multi-reporters",
    "reporterOptions": {
    "reporterEnabled": "spec, mocha-junit-reporter",
    "mochaJunitReporterReporterOptions": {
    "mochaFile": "cypress/results/results-[hash].xml"
    }

index.js file

"scripts": {
    "delete:reports": "rm cypress/results/* || true",
    "prereport": "delete:reports",
    "report": "cypress run --reporter cypress-multi-reporters --reporter-options mochaFile=cypress/results/results-[hash].xml"
  },
  "dependencies": {
       "cypress-multi-reporters": "^1.4.0",
       "junit-report-merger": "^0.0.6",
       "mocha": "^8.2.1",
       "mocha-junit-reporter": "^2.0.0",
   }

Command line (but it doesn't take the password so my tests all fail)

$ yarn report --env password=<password>
1

There are 1 answers

4
bhovhannes On BEST ANSWER

I've created a package specially for that purpose. It is called junit-report-merger.

You should write a Nodejs script which will use functions exported from that package:

merge.js

const path = require('path')
const {mergeFiles} = require('junit-report-merger')

const globby = require('globby')
const inputFiles = await globby(['results/report-*.xml'])

const outputFile = path.join(__dirname, 'results', 'combined-report.xml')

mergeFiles(
    outputFile,
    inputFiles,
    err => {
        if (err) {
            console.error(err)
        }
        else {
            console.log('successfully merged')
        }
    }
)

Once script is ready, you should run it after your tests. In your case, it will be something like this:

"scripts": {
    "report": "cypress run --reporter cypress-multi-reporters --reporter-options mochaFile=cypress/results/results-[hash].xml",
    "postreport": "node merge.js"
}

UPDATE

Just released version 1.0.0 of junit-report-merger, which has glob support, allows async/await and offers a CLI.

The code above still should work, but with that version, merge.js file from above can be written in a shorter way:

const path = require('path')
const {mergeFiles} = require('junit-report-merger')
    
const inputPattern = ['results/report-*.xml']

const outputFile = path.join(__dirname, 'results', 'combined-report.xml')

await mergeFiles(outputFile, inputPattern)
console.log('successfully merged')

But with version 1.0.0 you can avoid creating merge.js completely and use CLI instead.
Like this:

    "scripts": {
        "report": "cypress run --reporter cypress-multi-reporters --reporter-options mochaFile=cypress/results/results-[hash].xml",
        "postreport": "jrm ./results/combined-report.xml \"./cypress/results/results-*.xml\""
  }