11import { existsSync , promises as fs } from 'node:fs'
22import { dirname , resolve } from 'pathe'
33import type { Vitest } from '../../node'
4- import type { File , Reporter , Suite , Task , TaskState } from '../../types'
4+ import type { File , Reporter , SnapshotSummary , Suite , TaskState } from '../../types'
55import { getSuites , getTests } from '../../utils'
66import { getOutputFile } from '../../utils/config-helpers'
7- import { parseErrorStacktrace } from '../../utils/source-map'
87
98// for compatibility reasons, the reporter produces a JSON similar to the one produced by the Jest JSON reporter
109// the following types are extracted from the Jest repository (and simplified)
1110// the commented-out fields are the missing ones
1211
1312type Status = 'passed' | 'failed' | 'skipped' | 'pending' | 'todo' | 'disabled'
1413type Milliseconds = number
15- interface Callsite { line : number ; column : number }
14+ interface Callsite {
15+ line : number
16+ column : number
17+ }
18+
1619const StatusMap : Record < TaskState , Status > = {
1720 fail : 'failed' ,
1821 only : 'pending' ,
@@ -28,7 +31,7 @@ export interface JsonAssertionResult {
2831 status : Status
2932 title : string
3033 duration ?: Milliseconds | null
31- failureMessages : Array < string >
34+ failureMessages : Array < string > | null
3235 location ?: Callsite | null
3336}
3437
@@ -56,9 +59,9 @@ export interface JsonTestResults {
5659 startTime : number
5760 success : boolean
5861 testResults : Array < JsonTestResult >
62+ snapshot : SnapshotSummary
5963 // coverageMap?: CoverageMap | null | undefined
6064 // numRuntimeErrorTestSuites: number
61- // snapshot: SnapshotSummary
6265 // wasInterrupted: boolean
6366}
6467
@@ -104,7 +107,7 @@ export class JsonReporter implements Reporter {
104107
105108 const endTime = tests . reduce ( ( prev , next ) => Math . max ( prev , ( next . result ?. startTime ?? 0 ) + ( next . result ?. duration ?? 0 ) ) , startTime )
106109 const assertionResults = tests . map ( ( t ) => {
107- const ancestorTitles = [ ] as string [ ]
110+ const ancestorTitles : string [ ] = [ ]
108111 let iter : Suite | undefined = t . suite
109112 while ( iter ) {
110113 ancestorTitles . push ( iter . name )
@@ -114,13 +117,13 @@ export class JsonReporter implements Reporter {
114117
115118 return {
116119 ancestorTitles,
117- fullName : ancestorTitles . length > 0 ? ` ${ ancestorTitles . join ( ' ' ) } ${ t . name } ` : t . name ,
120+ fullName : t . name ? [ ... ancestorTitles , t . name ] . join ( ' ' ) : ancestorTitles . join ( ' ' ) ,
118121 status : StatusMap [ t . result ?. state || t . mode ] || 'skipped' ,
119122 title : t . name ,
120123 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
124127 } )
125128
126129 if ( tests . some ( t => t . result ?. state === 'run' ) ) {
@@ -153,6 +156,7 @@ export class JsonReporter implements Reporter {
153156 numFailedTests,
154157 numPendingTests,
155158 numTodoTests,
159+ snapshot : this . ctx . snapshot . summary ,
156160 startTime : this . start ,
157161 success,
158162 testResults,
@@ -187,21 +191,4 @@ export class JsonReporter implements Reporter {
187191 this . ctx . logger . log ( report )
188192 }
189193 }
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- }
207194}
0 commit comments