Skip to content

Commit d63e209

Browse files
committed
Parse attributeless <failure> tags correctly
[`jest-junit`](https://github.com/jest-community/jest-junit) generates failure tags that have no attributes, only inner text. For example: `<failure>Failed!</failure>`. The [`xml2js`](https://www.npmjs.com/package/xml2js) library seems to produce different objects based on whether a tag has attributes. See the minimal example: ```js var parseString = require('xml2js').parseString; var print = (err, result) => console.log(JSON.stringify(result, null, 2)) var parse = (xmlStr) => parseString(xmlStr, print) parse('<a b="c">d</a>') { "a": { "_": "d", "$": { "b": "c" } } } parse('<a>d</a>') { "a": "d" } ``` Notice how the second output above is not `{"a": {"_": "d"}}` With this change, we take into account this difference while parsing the failure messages.
1 parent 4cdf687 commit d63e209

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/test_parser.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,11 @@ async function parseJunitXml(xml: any): Promise<TestResult> {
250250
const element = failure_or_error[0]
251251

252252
message = element.$ ? element.$.message : undefined
253-
details = element._
253+
if (typeof element === "string") {
254+
details = element
255+
} else {
256+
details = element._
257+
}
254258

255259
counts.failed++
256260
} else {

test/junit.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,12 @@ describe("junit", async () => {
150150
it("parses testsuite with no failure message", async () => {
151151
const result = await parseJunitFile(`${resourcePath}/07-no-failure-message.xml`)
152152
})
153+
154+
it("parses attributeless failure tags", async () => {
155+
// https://github.com/jest-community/jest-junit generates failure tags
156+
// that have no attributes, only inner text.
157+
// Example: <failure>Failed!</failure>
158+
const result = await parseJunitFile(`${resourcePath}/08-failure-noattr-only-innertext.xml`)
159+
expect(result.suites[0].cases[0].details).to.eql("Failed!")
160+
})
153161
})
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<testsuites name="jest tests">
3+
<testsuite name="testsuite1">
4+
<testcase id="com.example.test1" name="Test 1" time="0.001">
5+
<failure>Failed!</failure>
6+
</testcase>
7+
</testsuite>
8+
</testsuites>

0 commit comments

Comments
 (0)