Skip to content

Commit 0417ba2

Browse files
authored
fix(vitest): correctly report failed test files as failures in json reporter, export json reporter types (#5081)
1 parent 5471cc3 commit 0417ba2

File tree

5 files changed

+26
-14
lines changed

5 files changed

+26
-14
lines changed

packages/vitest/src/node/reporters/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export {
2323
}
2424
export type { BaseReporter, Reporter }
2525

26+
export type { JsonAssertionResult, JsonTestResult, JsonTestResults } from './json'
27+
2628
export const ReportersMap = {
2729
'default': DefaultReporter,
2830
'basic': BasicReporter,

packages/vitest/src/node/reporters/json.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const StatusMap: Record<TaskState, Status> = {
2222
todo: 'todo',
2323
}
2424

25-
interface FormattedAssertionResult {
25+
export interface JsonAssertionResult {
2626
ancestorTitles: Array<string>
2727
fullName: string
2828
status: Status
@@ -32,18 +32,18 @@ interface FormattedAssertionResult {
3232
location?: Callsite | null
3333
}
3434

35-
interface FormattedTestResult {
35+
export interface JsonTestResult {
3636
message: string
3737
name: string
3838
status: 'failed' | 'passed'
3939
startTime: number
4040
endTime: number
41-
assertionResults: Array<FormattedAssertionResult>
41+
assertionResults: Array<JsonAssertionResult>
4242
// summary: string
4343
// coverage: unknown
4444
}
4545

46-
interface FormattedTestResults {
46+
export interface JsonTestResults {
4747
numFailedTests: number
4848
numFailedTestSuites: number
4949
numPassedTests: number
@@ -55,7 +55,7 @@ interface FormattedTestResults {
5555
numTotalTestSuites: number
5656
startTime: number
5757
success: boolean
58-
testResults: Array<FormattedTestResult>
58+
testResults: Array<JsonTestResult>
5959
// coverageMap?: CoverageMap | null | undefined
6060
// numRuntimeErrorTestSuites: number
6161
// snapshot: SnapshotSummary
@@ -83,7 +83,7 @@ export class JsonReporter implements Reporter {
8383
const numPassedTests = numTotalTests - numFailedTests
8484
const numPendingTests = tests.filter(t => t.result?.state === 'run').length
8585
const numTodoTests = tests.filter(t => t.mode === 'todo').length
86-
const testResults: Array<FormattedTestResult> = []
86+
const testResults: Array<JsonTestResult> = []
8787

8888
const success = numFailedTestSuites === 0 && numFailedTests === 0
8989

@@ -111,7 +111,7 @@ export class JsonReporter implements Reporter {
111111
duration: t.result?.duration,
112112
failureMessages: t.result?.errors?.map(e => e.message) || [],
113113
location: await this.getFailureLocation(t),
114-
} as FormattedAssertionResult
114+
} as JsonAssertionResult
115115
}))
116116

117117
if (tests.some(t => t.result?.state === 'run')) {
@@ -120,20 +120,21 @@ export class JsonReporter implements Reporter {
120120
+ 'Please report it to https://github.com/vitest-dev/vitest/issues')
121121
}
122122

123+
const hasFailedTests = tests.some(t => t.result?.state === 'fail')
124+
123125
testResults.push({
124126
assertionResults,
125127
startTime,
126128
endTime,
127-
status: tests.some(t =>
128-
t.result?.state === 'fail')
129+
status: file.result?.state === 'fail' || hasFailedTests
129130
? 'failed'
130131
: 'passed',
131132
message: file.result?.errors?.[0]?.message ?? '',
132133
name: file.filepath,
133134
})
134135
}
135136

136-
const result: FormattedTestResults = {
137+
const result: JsonTestResults = {
137138
numTotalTestSuites,
138139
numPassedTestSuites,
139140
numFailedTestSuites,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
throw new Error('failed test suite')

test/reporters/tests/html.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('html reporter', async () => {
4040
}, 120000)
4141

4242
it('resolves to "failing" status for test file "json-fail"', async () => {
43-
const [expected, testFile, basePath] = ['failing', 'json-fail', 'html/fail']
43+
const [expected, testFile, basePath] = ['failing', 'json-fail.test', 'html/fail']
4444

4545
await runVitest({ reporters: 'html', outputFile: `${basePath}/index.html`, root }, [testFile])
4646

test/reporters/tests/json.test.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,18 @@ describe('json reporter', async () => {
1111

1212
const data = JSON.parse(stdout)
1313

14-
expect(data.testResults).toHaveLength(1)
15-
expect(data.testResults[0].assertionResults).toHaveLength(1)
14+
expect(data.testResults).toHaveLength(2)
15+
16+
const failedImport = data.testResults.find((r: any) => r.name.includes('json-fail-import.test'))!
17+
const failedTest = data.testResults.find((r: any) => r.name.includes('json-fail.test'))!
18+
19+
expect(failedTest.assertionResults).toHaveLength(1)
20+
expect(failedImport.assertionResults).toHaveLength(0)
21+
22+
expect(failedTest.status).toBe('failed')
23+
expect(failedImport.status).toBe('failed')
1624

17-
const result = data.testResults[0].assertionResults[0]
25+
const result = failedTest.assertionResults[0]
1826
delete result.duration
1927
expect(result).toMatchSnapshot()
2028
}, 40000)

0 commit comments

Comments
 (0)