Sitelet https://github.com/dorny/test-reporter/commit/f8ae4deee68dd1974e29d2b1bc34968efc91fb09
Skip to content

Commit f8ae4de

Browse files
committed
Report jest-junit testsuite errors as failures
Test errors are different (represented differently in the JUnit XML output from jest-junit) to test _failures_. Failures are tests which ran and failed, errors are for tests/test suites which did not even run because the test code itself didn't build or didn't execute correctly. jest-junit has an option to enable reporting of test suite errors, but test-reporter then interprets these as successful tests.
1 parent 540c280 commit f8ae4de

7 files changed

Lines changed: 141 additions & 3 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
![Tests failed](https://img.shields.io/badge/tests-2%20failed-critical)
2+
|Report|Passed|Failed|Skipped|Time|
3+
|:---|---:|---:|---:|---:|
4+
|[fixtures/test-errors/jest/jest-test-results.xml](#user-content-r0)||2 ❌||646ms|
5+
## ❌ <a id="user-content-r0" href="#user-content-r0">fixtures/test-errors/jest/jest-test-results.xml</a>
6+
**2** tests were completed in **646ms** with **0** passed, **2** failed and **0** skipped.
7+
|Test suite|Passed|Failed|Skipped|Time|
8+
|:---|---:|---:|---:|---:|
9+
|[libs/bar.spec.ts](#user-content-r0s0)||1 ❌||0ms|
10+
|[libs/foo.spec.ts](#user-content-r0s1)||1 ❌||0ms|
11+
### ❌ <a id="user-content-r0s0" href="#user-content-r0s0">libs/bar.spec.ts</a>
12+
```
13+
Test suite failed to run
14+
❌ libs/bar.spec.ts
15+
● Test suite failed to run
16+
```
17+
### ❌ <a id="user-content-r0s1" href="#user-content-r0s1">libs/foo.spec.ts</a>
18+
```
19+
Test suite failed to run
20+
❌ libs/foo.spec.ts
21+
● Test suite failed to run
22+
```

‎__tests__/__snapshots__/jest-junit.test.ts.snap‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,68 @@
11
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

3+
exports[`jest-junit tests jest testsuite errors example test results matches snapshot 1`] = `
4+
TestRunResult {
5+
"path": "fixtures/test-errors/jest/jest-test-results.xml",
6+
"suites": [
7+
TestSuiteResult {
8+
"groups": [
9+
TestGroupResult {
10+
"name": "Test suite failed to run",
11+
"tests": [
12+
TestCaseResult {
13+
"error": {
14+
"details": " ● Test suite failed to run
15+
16+
tsconfig.json:13:3 - error TS6258: 'typeRoots' should be set inside the 'compilerOptions' object of the config json file
17+
18+
13 "typeRoots": ["./src/lib/types", "./node_modules/@types"],
19+
~~~~~~~~~~~
20+
",
21+
"line": undefined,
22+
"path": undefined,
23+
},
24+
"name": "libs/foo.spec.ts",
25+
"result": "failed",
26+
"time": 0,
27+
},
28+
],
29+
},
30+
],
31+
"name": "libs/foo.spec.ts",
32+
"totalTime": 0,
33+
},
34+
TestSuiteResult {
35+
"groups": [
36+
TestGroupResult {
37+
"name": "Test suite failed to run",
38+
"tests": [
39+
TestCaseResult {
40+
"error": {
41+
"details": " ● Test suite failed to run
42+
43+
tsconfig.json:13:3 - error TS6258: 'typeRoots' should be set inside the 'compilerOptions' object of the config json file
44+
45+
13 "typeRoots": ["./src/lib/types", "./node_modules/@types"],
46+
~~~~~~~~~~~
47+
",
48+
"line": undefined,
49+
"path": undefined,
50+
},
51+
"name": "libs/bar.spec.ts",
52+
"result": "failed",
53+
"time": 0,
54+
},
55+
],
56+
},
57+
],
58+
"name": "libs/bar.spec.ts",
59+
"totalTime": 0,
60+
},
61+
],
62+
"totalTime": 646,
63+
}
64+
`;
65+
366
exports[`jest-junit tests parsing ESLint report without timing information works - PR #134 1`] = `
467
TestRunResult {
568
"path": "fixtures/jest-junit-eslint.xml",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
libs/bar.spec.ts
2+
libs/foo.spec.ts
3+
tsconfig.json
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<testsuites name="jest tests" tests="0" failures="0" errors="2" time="0.646">
3+
<testsuite name="libs/foo.spec.ts" errors="1" failures="0" skipped="0" timestamp="1970-01-01T00:00:00" time="0" tests="0">
4+
<testcase classname="Test suite failed to run" name="libs/foo.spec.ts" time="0">
5+
<error> ● Test suite failed to run
6+
7+
tsconfig.json:13:3 - error TS6258: &apos;typeRoots&apos; should be set inside the &apos;compilerOptions&apos; object of the config json file
8+
9+
13 &quot;typeRoots&quot;: [&quot;./src/lib/types&quot;, &quot;./node_modules/@types&quot;],
10+
~~~~~~~~~~~
11+
</error>
12+
</testcase>
13+
</testsuite>
14+
<testsuite name="libs/bar.spec.ts" errors="1" failures="0" skipped="0" timestamp="1970-01-01T00:00:00" time="0" tests="0">
15+
<testcase classname="Test suite failed to run" name="libs/bar.spec.ts" time="0">
16+
<error> ● Test suite failed to run
17+
18+
tsconfig.json:13:3 - error TS6258: &apos;typeRoots&apos; should be set inside the &apos;compilerOptions&apos; object of the config json file
19+
20+
13 &quot;typeRoots&quot;: [&quot;./src/lib/types&quot;, &quot;./node_modules/@types&quot;],
21+
~~~~~~~~~~~
22+
</error>
23+
</testcase>
24+
</testsuite>
25+
</testsuites>

‎__tests__/jest-junit.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,4 +351,27 @@ describe('jest-junit tests', () => {
351351
// Report should have the title as the first line
352352
expect(report).toMatch(/^# My Custom Title\n## 1 passed, 4 failed and 1 skipped\n/)
353353
})
354+
355+
it('jest testsuite errors example test results matches snapshot', async () => {
356+
const fixturePath = path.join(__dirname, 'fixtures', 'test-errors', 'jest', 'jest-test-results.xml')
357+
const trackedFilesPath = path.join(__dirname, 'fixtures', 'test-errors', 'jest', 'files.txt')
358+
const outputPath = path.join(__dirname, '__outputs__', 'jest-test-errors-results.md')
359+
const filePath = normalizeFilePath(path.relative(__dirname, fixturePath))
360+
const fileContent = fs.readFileSync(fixturePath, {encoding: 'utf8'})
361+
362+
const trackedFiles = fs.readFileSync(trackedFilesPath, {encoding: 'utf8'}).split(/\n\r?/g)
363+
const opts: ParseOptions = {
364+
parseErrors: true,
365+
trackedFiles
366+
//workDir: '/home/dorny/dorny/jest/'
367+
}
368+
369+
const parser = new JestJunitParser(opts)
370+
const result = await parser.parse(filePath, fileContent)
371+
expect(result).toMatchSnapshot()
372+
373+
const report = getReport([result])
374+
fs.mkdirSync(path.dirname(outputPath), {recursive: true})
375+
fs.writeFileSync(outputPath, report)
376+
})
354377
})

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,18 @@ export class JestJunitParser implements TestParser {
7575
}
7676

7777
private getTestCaseResult(test: TestCase): TestExecutionResult {
78-
if (test.failure) return 'failed'
78+
if (test.failure || test.error) return 'failed'
7979
if (test.skipped) return 'skipped'
8080
return 'success'
8181
}
8282

8383
private getTestCaseError(tc: TestCase): TestCaseError | undefined {
84-
if (!this.options.parseErrors || !tc.failure) {
84+
if (!this.options.parseErrors || !(tc.failure || tc.error)) {
8585
return undefined
8686
}
8787

88-
const details = typeof tc.failure[0] === 'string' ? tc.failure[0] : tc.failure[0]['_']
88+
const message = tc.failure ? tc.failure[0] : tc.error ? tc.error[0] : 'unknown failure'
89+
const details = typeof message === 'string' ? message : message['_']
8990
let path
9091
let line
9192

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ export interface TestCase {
3131
}
3232
failure?: string[]
3333
skipped?: string[]
34+
error?: string[]
3435
}

0 commit comments

Comments
 (0)