Skip to content

Commit

Permalink
Merge pull request #193 from rvdlaarschot/mocha-empty-test-suite
Browse files Browse the repository at this point in the history
Gracefully handle empty nested testsuite elements for JUnit.
  • Loading branch information
dorny committed Nov 13, 2022
2 parents 2f63fb8 + 3b54f63 commit 074fe2c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
5 changes: 5 additions & 0 deletions __tests__/fixtures/empty/jest-junit-empty-testsuite.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="jest tests" tests="0" failures="0" errors="0" time="11.299">
<testsuite name="__tests__\main.test.js" errors="0" failures="0" skipped="0" timestamp="2020-10-27T21:39:41" time="0.486" tests="0">
</testsuite>
</testsuites>
18 changes: 17 additions & 1 deletion __tests__/jest-junit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {getReport} from '../src/report/get-report'
import {normalizeFilePath} from '../src/utils/path-utils'

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

it('produces empty test run result when there are no test cases in a nested testsuite element', async () => {
const fixturePath = path.join(__dirname, 'fixtures', 'empty', 'jest-junit-empty-testsuite.xml')
const filePath = normalizeFilePath(path.relative(__dirname, fixturePath))
const fileContent = fs.readFileSync(fixturePath, {encoding: 'utf8'})

const opts: ParseOptions = {
parseErrors: true,
trackedFiles: []
}

const parser = new JestJunitParser(opts)
const result = await parser.parse(filePath, fileContent)
expect(result.tests).toBe(0)
expect(result.result).toBe('success')
})

it('report from ./reports/jest test results matches snapshot', async () => {
const fixturePath = path.join(__dirname, 'fixtures', 'jest-junit.xml')
const outputPath = path.join(__dirname, '__outputs__', 'jest-junit.md')
Expand Down
3 changes: 3 additions & 0 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/parsers/jest-junit/jest-junit-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ export class JestJunitParser implements TestParser {
}

private getGroups(suite: TestSuite): TestGroupResult[] {
if (!suite.testcase) {
return []
}

const groups: {describe: string; tests: TestCase[]}[] = []
for (const tc of suite.testcase) {
let grp = groups.find(g => g.describe === tc.$.classname)
Expand Down
2 changes: 1 addition & 1 deletion src/parsers/jest-junit/jest-junit-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface TestSuite {
time: string
timestamp?: Date
}
testcase: TestCase[]
testcase?: TestCase[]
}

export interface TestCase {
Expand Down

0 comments on commit 074fe2c

Please sign in to comment.