3

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>
user3601310
  • 795
  • 2
  • 11
  • 18

1 Answers1

11

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\""
  }
bhovhannes
  • 5,511
  • 2
  • 27
  • 37
  • Thanks a lot @bhovhannes :D it was a great help! – user3601310 Nov 25 '20 at 20:47
  • I also found other solution that is simpler? I'd say? but first I need to install junit-merger and run on Command line yarn junit-merger --d solved merging issue :) – user3601310 Nov 26 '20 at 02:25
  • junit-merger seems to do the same as jrm cli introduced in v1.0.0. But as you already said, you have to install it first. With npm package it is easy to install and upgrade later. Did you experienced any issues with jrm cli? – bhovhannes Nov 26 '20 at 05:38
  • Also, the code of junit-merger is very different from mine, so they will produce different merge results. I am not sure which one is more correct. Mine worked in my case, and I am sure junit-merger worked for its author as well. I am ready to improve my implementation, so let me know if you see something that does not work in your case – bhovhannes Nov 26 '20 at 06:00