Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle case when testsuites is not the root element of the JUnit xml file #85

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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