From a3356fa6395a029437a791d0b26c170a5f81c673 Mon Sep 17 00:00:00 2001 From: Ramon van de Laarschot Date: Fri, 9 Sep 2022 16:51:32 +0200 Subject: [PATCH 1/2] Gracefully handle empty nested testsuite elements for JUnit. This fixes an issue with mocha-junit-reporter returning empty root-level testsuite elements. --- .../empty/jest-junit-empty-testsuite.xml | 5 +++++ __tests__/jest-junit.test.ts | 18 +++++++++++++++++- src/parsers/jest-junit/jest-junit-parser.ts | 4 ++++ src/parsers/jest-junit/jest-junit-types.ts | 2 +- 4 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 __tests__/fixtures/empty/jest-junit-empty-testsuite.xml diff --git a/__tests__/fixtures/empty/jest-junit-empty-testsuite.xml b/__tests__/fixtures/empty/jest-junit-empty-testsuite.xml new file mode 100644 index 0000000..ad54653 --- /dev/null +++ b/__tests__/fixtures/empty/jest-junit-empty-testsuite.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/__tests__/jest-junit.test.ts b/__tests__/jest-junit.test.ts index 0a5703e..b6533e6 100644 --- a/__tests__/jest-junit.test.ts +++ b/__tests__/jest-junit.test.ts @@ -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'}) @@ -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') diff --git a/src/parsers/jest-junit/jest-junit-parser.ts b/src/parsers/jest-junit/jest-junit-parser.ts index 11fde4f..cbfe0ae 100644 --- a/src/parsers/jest-junit/jest-junit-parser.ts +++ b/src/parsers/jest-junit/jest-junit-parser.ts @@ -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) diff --git a/src/parsers/jest-junit/jest-junit-types.ts b/src/parsers/jest-junit/jest-junit-types.ts index 713ded0..3ca03b9 100644 --- a/src/parsers/jest-junit/jest-junit-types.ts +++ b/src/parsers/jest-junit/jest-junit-types.ts @@ -19,7 +19,7 @@ export interface TestSuite { time: string timestamp?: Date } - testcase: TestCase[] + testcase?: TestCase[] } export interface TestCase { From 3b54f63d958d702095c7959fd8e1ba59fee51061 Mon Sep 17 00:00:00 2001 From: Michal Dorner Date: Sun, 13 Nov 2022 13:22:18 +0100 Subject: [PATCH 2/2] Update dist/index.js --- dist/index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dist/index.js b/dist/index.js index dcb19c3..f9222d3 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1163,6 +1163,9 @@ class JestJunitParser { return new test_results_1.TestRunResult(path, suites, time); } getGroups(suite) { + if (!suite.testcase) { + return []; + } const groups = []; for (const tc of suite.testcase) { let grp = groups.find(g => g.describe === tc.$.classname);