Skip to content

Commit 074fe2c

Browse files
authored
Merge pull request #193 from rvdlaarschot/mocha-empty-test-suite
Gracefully handle empty nested testsuite elements for JUnit.
2 parents 2f63fb8 + 3b54f63 commit 074fe2c

File tree

5 files changed

+30
-2
lines changed

5 files changed

+30
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<testsuites name="jest tests" tests="0" failures="0" errors="0" time="11.299">
3+
<testsuite name="__tests__\main.test.js" errors="0" failures="0" skipped="0" timestamp="2020-10-27T21:39:41" time="0.486" tests="0">
4+
</testsuite>
5+
</testsuites>

__tests__/jest-junit.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {getReport} from '../src/report/get-report'
77
import {normalizeFilePath} from '../src/utils/path-utils'
88

99
describe('jest-junit tests', () => {
10-
it('produces empty test run result when there are no test cases', async () => {
10+
it('produces empty test run result when there are no test cases in the testsuites element', async () => {
1111
const fixturePath = path.join(__dirname, 'fixtures', 'empty', 'jest-junit.xml')
1212
const filePath = normalizeFilePath(path.relative(__dirname, fixturePath))
1313
const fileContent = fs.readFileSync(fixturePath, {encoding: 'utf8'})
@@ -23,6 +23,22 @@ describe('jest-junit tests', () => {
2323
expect(result.result).toBe('success')
2424
})
2525

26+
it('produces empty test run result when there are no test cases in a nested testsuite element', async () => {
27+
const fixturePath = path.join(__dirname, 'fixtures', 'empty', 'jest-junit-empty-testsuite.xml')
28+
const filePath = normalizeFilePath(path.relative(__dirname, fixturePath))
29+
const fileContent = fs.readFileSync(fixturePath, {encoding: 'utf8'})
30+
31+
const opts: ParseOptions = {
32+
parseErrors: true,
33+
trackedFiles: []
34+
}
35+
36+
const parser = new JestJunitParser(opts)
37+
const result = await parser.parse(filePath, fileContent)
38+
expect(result.tests).toBe(0)
39+
expect(result.result).toBe('success')
40+
})
41+
2642
it('report from ./reports/jest test results matches snapshot', async () => {
2743
const fixturePath = path.join(__dirname, 'fixtures', 'jest-junit.xml')
2844
const outputPath = path.join(__dirname, '__outputs__', 'jest-junit.md')

dist/index.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/parsers/jest-junit/jest-junit-parser.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ export class JestJunitParser implements TestParser {
4848
}
4949

5050
private getGroups(suite: TestSuite): TestGroupResult[] {
51+
if (!suite.testcase) {
52+
return []
53+
}
54+
5155
const groups: {describe: string; tests: TestCase[]}[] = []
5256
for (const tc of suite.testcase) {
5357
let grp = groups.find(g => g.describe === tc.$.classname)

src/parsers/jest-junit/jest-junit-types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export interface TestSuite {
1919
time: string
2020
timestamp?: Date
2121
}
22-
testcase: TestCase[]
22+
testcase?: TestCase[]
2323
}
2424

2525
export interface TestCase {

0 commit comments

Comments
 (0)