From d63e20938209edfb73731a0f8265770e8e72f72b Mon Sep 17 00:00:00 2001 From: Sujeet Gholap Date: Sat, 30 Nov 2024 20:24:26 -0600 Subject: [PATCH] Parse attributeless tags correctly [`jest-junit`](https://github.com/jest-community/jest-junit) generates failure tags that have no attributes, only inner text. For example: `Failed!`. 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('d') { "a": { "_": "d", "$": { "b": "c" } } } parse('d') { "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. --- src/test_parser.ts | 6 +++++- test/junit.ts | 8 ++++++++ test/resources/junit/08-failure-noattr-only-innertext.xml | 8 ++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 test/resources/junit/08-failure-noattr-only-innertext.xml diff --git a/src/test_parser.ts b/src/test_parser.ts index d94e21b..9798822 100644 --- a/src/test_parser.ts +++ b/src/test_parser.ts @@ -250,7 +250,11 @@ async function parseJunitXml(xml: any): Promise { 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 { diff --git a/test/junit.ts b/test/junit.ts index 204d8b1..df1e5aa 100644 --- a/test/junit.ts +++ b/test/junit.ts @@ -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: Failed! + const result = await parseJunitFile(`${resourcePath}/08-failure-noattr-only-innertext.xml`) + expect(result.suites[0].cases[0].details).to.eql("Failed!") + }) }) diff --git a/test/resources/junit/08-failure-noattr-only-innertext.xml b/test/resources/junit/08-failure-noattr-only-innertext.xml new file mode 100644 index 0000000..3835ca1 --- /dev/null +++ b/test/resources/junit/08-failure-noattr-only-innertext.xml @@ -0,0 +1,8 @@ + + + + + Failed! + + +