Skip to content

Commit

Permalink
Handle case when testsuites is not the root element of the JUnit xml …
Browse files Browse the repository at this point in the history
…file (#85)

* Update junit.ts

* Update junit.test.ts
  • Loading branch information
JakeFDev authored Apr 30, 2024
1 parent c2d5cfd commit 11c181e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
12 changes: 12 additions & 0 deletions __tests__/junit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ describe('parsing junit', () => {
'Parse JUnit report. Non-whitespace before first tag.\nLine: 0\nColumn: 1\nChar: b'
)
})

test('should work with omitted parent testsuites element', async () => {
const xml =
'<?xml version="1.0" encoding="UTF-8"?><testsuite name="should test controller" errors="0" failures="0" skipped="0" timestamp="2022-03-21T21:15:26" time="0.981" tests="2"><testcase classname="should test controller when #getPost method method succeeds" name="should test controller when #getPost method method succeeds" time="0.004"></testcase><testcase classname="should test controller when #getPost method method fails" name="should test controller when #getPost method method fails" time="0.001"></testcase></testsuite>'
const junit = await parseJunit(xml)

expect(junit?.skipped).toBe(0)
expect(junit?.errors).toBe(0)
expect(junit?.failures).toBe(0)
expect(junit?.tests).toBe(2)
expect(junit?.time).toBe(0.981)
})
})

describe('parse junit and check report output', () => {
Expand Down
14 changes: 12 additions & 2 deletions src/junit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,18 @@ export async function parseJunit(xmlContent: string): Promise<Junit | null> {
return null
}

const main = parsedJunit.testsuites['$']
const testsuites = parsedJunit.testsuites.testsuite
/**
* <testsuites> Usually the root element of a JUnit XML file. Some tools leave out
* the <testsuites> element if there is only a single top-level <testsuite> element (which
* is then used as the root element).
*/
const main = parsedJunit.testsuites?.$ ?? parsedJunit.testsuite?.$
const testsuites = parsedJunit.testsuites?.testsuite
? parsedJunit.testsuites?.testsuite
: parsedJunit.testsuite
? [parsedJunit.testsuite]
: null

const errors =
testsuites
?.map((t: any) => Number(t['$'].errors))
Expand Down

0 comments on commit 11c181e

Please sign in to comment.