michaelleeallen / mocha-junit-reporter

A JUnit XML reporter for mocha.
MIT License
144 stars 153 forks source link

Option to add filename to all testsuites #132

Open tomdohnal opened 4 years ago

tomdohnal commented 4 years ago

Hi :)

I'm using this reporter in order to set up parallel tests in CircleCi. (I want to split them up by the time each of them takes to run).

However, CircleCI expects the test suites to include the file as property and this reporter only add it to the "root test suite".

I'd like to have the option to make the reporter add the file property to all the test suites.

I'm happy to send a PR if that's something you'd be OK with.

schuylr commented 4 years ago

@tomdohnal Here's a script I just wrote to fix this issue in the meantime. You can run it as a post-step in CircleCI e.g. yarn node scripts/fix-junit-xml.js

Change the ./cypress/results to wherever you're outputting your junit files.

const fs = require("fs")
const parseString = require("xml2js").parseString
const xml2js = require("xml2js")

fs.readdir("./cypress/results", (err, files) => {
  if (err) {
    return console.log(err)
  }
  files.forEach((file) => {
    const filePath = `./cypress/results/${file}`
    fs.readFile(filePath, "utf8", (err, data) => {
      if (err) {
        return console.log(err)
      }

      parseString(data, (err, xml) => {
        if (err) {
          return console.log(err)
        }

        const file = xml.testsuites.testsuite[0].$.file
        xml.testsuites.testsuite.forEach((testsuite, index) => {
          if (index > 0) {
            testsuite.$.file = file
          }
        })

        const builder = new xml2js.Builder()
        const xmlOut = builder.buildObject(xml)
        fs.writeFile(filePath, xmlOut, (err) => {
          if (err) throw err
        })
      })
    })
  })
})
kjvarga commented 3 years ago

I'm using sed in CircleCI to post-process the report XML file. It's quite a simple approach. Note that the step needs to always run - on pass or failure of the tests - which is why I use when: always. This step comes right after the step that runs the unit tests.

          name: Process test report
          when: always
          command: |
            # Convert absolute paths to relative to support splitting tests by timing
            if [ -e ./log/test-results/mocha/test-results.xml ]; then
              sed -i "s|`pwd`/||g" ./log/test-results/mocha/test-results.xml
            fi
sebastian-lerner commented 1 year ago

thanks @schuylr for the workaround! it worked for my team

anybody know who might be the right person to discuss the possibility of avoiding this workaround? =)