Skip to content

Commit

Permalink
fix: support reports without root testsuites element (related to #239)
Browse files Browse the repository at this point in the history
  • Loading branch information
bhovhannes committed Apr 3, 2023
1 parent a5ed559 commit b15f689
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Merges multiple JUnit XML reports into one.

Reporters of many testing frameworks generate JUnit XML reports. [`mocha-junit-reporter`](https://www.npmjs.com/package/mocha-junit-reporter), [`karma-junit-reporter`](https://www.npmjs.com/package/karma-junit-reporter) to name a few. Sometimes there is a need to combine multiple reports together in a single file. This is what `junit-report-merger` does.

`junit-report-merger` creates a new test results report in JUnit XML format by collecting all `<testsuite>` elements from all XML reports and putting them together.
`junit-report-merger` creates a new test results report in [JUnit XML format](#junit-xml-format) by collecting all `<testsuite>` elements from all XML reports and putting them together.

## CLI

Expand Down Expand Up @@ -206,6 +206,16 @@ Merges given XML strings and returns the result.
| srcStrings | <code>string[]</code> | Array of strings to merge together. |
| [options] | <code>object</code> | Merge options. Currently unused. |

## JUnit XML Format

Unfortunately, there is no official specification of JUnit XML file format.

The XML schema for the original JUnit XML format is [here](https://github.com/windyroad/JUnit-Schema/blob/master/JUnit.xsd).

Over the time, various CI tools and test management software augmented original format with their own properties.
The most comprehensive overview of the format is put together by folks at Testmo [here](https://github.com/testmoapp/junitxml).
`jrm` produces output conforming to that format and accepts files conforming to that format.

## License

MIT (http://www.opensource.org/licenses/mit-license.php)
Expand Down
5 changes: 5 additions & 0 deletions src/domHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ function isTestSuiteNode(node) {
return node.nodeName.toLowerCase() === 'testsuite'
}

function isTestSuitesNode(node) {
return node.nodeName.toLowerCase() === 'testsuites'
}

function findTestSuiteByName(builder, suiteName) {
return builder.find(
({ node }) => isTestSuiteNode(node) && suiteName === getNodeAttribute(node, 'name'),
Expand All @@ -21,5 +25,6 @@ function findTestSuiteByName(builder, suiteName) {
module.exports = {
findTestSuiteByName,
isTestSuiteNode,
isTestSuitesNode,
getNodeAttribute
}
21 changes: 19 additions & 2 deletions src/mergeToString.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
const { create } = require('xmlbuilder2')
const { KNOWN_ATTRIBUTES } = require('./attributes.js')
const { isNumeric } = require('./helpers.js')
const { getNodeAttribute, findTestSuiteByName, isTestSuiteNode } = require('./domHelpers.js')
const {
getNodeAttribute,
findTestSuiteByName,
isTestSuiteNode,
isTestSuitesNode,
createEmptyBuilder
} = require('./domHelpers.js')

/**
* @typedef {{}} MergeStringsOptions
Expand Down Expand Up @@ -71,12 +77,23 @@ module.exports.mergeToString = function (srcStrings, options) {
)
}

let srcBuilder = create(srcString)
if (!isTestSuitesNode(srcBuilder.root().node)) {
srcBuilder = create(
{
encoding: 'UTF-8'
},
{
testsuites: [srcBuilder.toObject()]
}
)
}
visitNodesRecursively(
{
currentPath: [],
targetBuilder: targetDoc.root()
},
create(srcString).root()
srcBuilder.root()
)
})

Expand Down
16 changes: 7 additions & 9 deletions test/fixtures/testsuite-merging/5/file1.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="" tests="1" errors="0" time="3">
<testcase name="testA-1" time="2" />
<testsuite name="subA" tests="2" errors="0" time="3">
<testcase name="test-subA-1" time="1" />
<testcase name="test-subA-2" time="3" />
</testsuite>
<testcase name="testA-2" time="2" />
<testsuite name="" tests="1" errors="0" time="3">
<testcase name="testA-1" time="2" />
<testsuite name="subA" tests="2" errors="0" time="3">
<testcase name="test-subA-1" time="1" />
<testcase name="test-subA-2" time="3" />
</testsuite>
</testsuites>
<testcase name="testA-2" time="2" />
</testsuite>

0 comments on commit b15f689

Please sign in to comment.