1
1
import { existsSync , promises as fs } from 'node:fs'
2
2
import { dirname , resolve } from 'pathe'
3
3
import type { Vitest } from '../../node'
4
- import type { File , Reporter , Suite , Task , TaskState } from '../../types'
4
+ import type { File , Reporter , SnapshotSummary , Suite , TaskState } from '../../types'
5
5
import { getSuites , getTests } from '../../utils'
6
6
import { getOutputFile } from '../../utils/config-helpers'
7
- import { parseErrorStacktrace } from '../../utils/source-map'
8
7
9
8
// for compatibility reasons, the reporter produces a JSON similar to the one produced by the Jest JSON reporter
10
9
// the following types are extracted from the Jest repository (and simplified)
11
10
// the commented-out fields are the missing ones
12
11
13
12
type Status = 'passed' | 'failed' | 'skipped' | 'pending' | 'todo' | 'disabled'
14
13
type Milliseconds = number
15
- interface Callsite { line : number ; column : number }
14
+ interface Callsite {
15
+ line : number
16
+ column : number
17
+ }
18
+
16
19
const StatusMap : Record < TaskState , Status > = {
17
20
fail : 'failed' ,
18
21
only : 'pending' ,
@@ -28,7 +31,7 @@ export interface JsonAssertionResult {
28
31
status : Status
29
32
title : string
30
33
duration ?: Milliseconds | null
31
- failureMessages : Array < string >
34
+ failureMessages : Array < string > | null
32
35
location ?: Callsite | null
33
36
}
34
37
@@ -56,9 +59,9 @@ export interface JsonTestResults {
56
59
startTime : number
57
60
success : boolean
58
61
testResults : Array < JsonTestResult >
62
+ snapshot : SnapshotSummary
59
63
// coverageMap?: CoverageMap | null | undefined
60
64
// numRuntimeErrorTestSuites: number
61
- // snapshot: SnapshotSummary
62
65
// wasInterrupted: boolean
63
66
}
64
67
@@ -104,7 +107,7 @@ export class JsonReporter implements Reporter {
104
107
105
108
const endTime = tests . reduce ( ( prev , next ) => Math . max ( prev , ( next . result ?. startTime ?? 0 ) + ( next . result ?. duration ?? 0 ) ) , startTime )
106
109
const assertionResults = tests . map ( ( t ) => {
107
- const ancestorTitles = [ ] as string [ ]
110
+ const ancestorTitles : string [ ] = [ ]
108
111
let iter : Suite | undefined = t . suite
109
112
while ( iter ) {
110
113
ancestorTitles . push ( iter . name )
@@ -114,13 +117,13 @@ export class JsonReporter implements Reporter {
114
117
115
118
return {
116
119
ancestorTitles,
117
- fullName : ancestorTitles . length > 0 ? ` ${ ancestorTitles . join ( ' ' ) } ${ t . name } ` : t . name ,
120
+ fullName : t . name ? [ ... ancestorTitles , t . name ] . join ( ' ' ) : ancestorTitles . join ( ' ' ) ,
118
121
status : StatusMap [ t . result ?. state || t . mode ] || 'skipped' ,
119
122
title : t . name ,
120
123
duration : t . result ?. duration ,
121
- failureMessages : t . result ?. errors ?. map ( e => e . message ) || [ ] ,
122
- location : this . getFailureLocation ( t ) ,
123
- } as JsonAssertionResult
124
+ failureMessages : t . result ?. errors ?. map ( e => e . stack || e . message ) || [ ] ,
125
+ location : t . location ,
126
+ } satisfies JsonAssertionResult
124
127
} )
125
128
126
129
if ( tests . some ( t => t . result ?. state === 'run' ) ) {
@@ -153,6 +156,7 @@ export class JsonReporter implements Reporter {
153
156
numFailedTests,
154
157
numPendingTests,
155
158
numTodoTests,
159
+ snapshot : this . ctx . snapshot . summary ,
156
160
startTime : this . start ,
157
161
success,
158
162
testResults,
@@ -187,21 +191,4 @@ export class JsonReporter implements Reporter {
187
191
this . ctx . logger . log ( report )
188
192
}
189
193
}
190
-
191
- protected getFailureLocation ( test : Task ) : Callsite | undefined {
192
- const error = test . result ?. errors ?. [ 0 ]
193
- if ( ! error )
194
- return
195
-
196
- const project = this . ctx . getProjectByTaskId ( test . id )
197
- const stack = parseErrorStacktrace ( error , {
198
- getSourceMap : file => project . getBrowserSourceMapModuleById ( file ) ,
199
- frameFilter : this . ctx . config . onStackTrace ,
200
- } )
201
- const frame = stack [ 0 ]
202
- if ( ! frame )
203
- return
204
-
205
- return { line : frame . line , column : frame . column }
206
- }
207
194
}
0 commit comments