Skip to content

Commit

Permalink
Parse attributeless <failure> tags correctly
Browse files Browse the repository at this point in the history
[`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.
  • Loading branch information
sujeet committed Dec 1, 2024
1 parent 4cdf687 commit d63e209
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/test_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,11 @@ async function parseJunitXml(xml: any): Promise<TestResult> {
const element = failure_or_error[0]

message = element.$ ? element.$.message : undefined
details = element._
if (typeof element === "string") {
details = element
} else {
details = element._
}

counts.failed++
} else {
Expand Down
8 changes: 8 additions & 0 deletions test/junit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,12 @@ describe("junit", async () => {
it("parses testsuite with no failure message", async () => {
const result = await parseJunitFile(`${resourcePath}/07-no-failure-message.xml`)
})

it("parses attributeless failure tags", async () => {
// https://github.com/jest-community/jest-junit generates failure tags
// that have no attributes, only inner text.
// Example: <failure>Failed!</failure>
const result = await parseJunitFile(`${resourcePath}/08-failure-noattr-only-innertext.xml`)
expect(result.suites[0].cases[0].details).to.eql("Failed!")
})
})
8 changes: 8 additions & 0 deletions test/resources/junit/08-failure-noattr-only-innertext.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="jest tests">
<testsuite name="testsuite1">
<testcase id="com.example.test1" name="Test 1" time="0.001">
<failure>Failed!</failure>
</testcase>
</testsuite>
</testsuites>

0 comments on commit d63e209

Please sign in to comment.