@@ -33,9 +33,12 @@ import mime from 'mime/lite'
3333import { basename , extname , resolve } from 'pathe'
3434
3535export class TestRun {
36+ private externalSourceMaps = new Map < string , unknown > ( )
37+
3638 constructor ( private vitest : Vitest ) { }
3739
3840 async start ( specifications : TestSpecification [ ] ) : Promise < void > {
41+ this . externalSourceMaps . clear ( )
3942 const filepaths = specifications . map ( ( spec ) => spec . moduleId )
4043 this . vitest . state . collectPaths ( filepaths )
4144
@@ -108,7 +111,7 @@ export class TestRun {
108111 ? project . browser ?. parseStacktrace ( entry . stack , { firstOnly : true } ) || [ ]
109112 : parseStacktrace ( entry . stack , {
110113 firstOnly : true ,
111- getSourceMap : ( file ) => getExternalSourceMap ( project , file ) ,
114+ getSourceMap : ( file ) => this . getExternalSourceMap ( project , file ) ,
112115 } )
113116 if ( stack ) {
114117 entry . location = { file : stack . file , line : stack . line , column : stack . column }
@@ -194,12 +197,20 @@ export class TestRun {
194197 update . forEach ( ( [ taskId , result ] ) => {
195198 const task = this . vitest . state . idMap . get ( taskId )
196199 const isBrowser = task && task . file . pool === 'browser'
200+ // the worker sends all errors of the task again in each update
201+ const previous = task ?. result
197202
198- result ?. errors ?. forEach ( ( error ) => {
203+ result ?. errors ?. forEach ( ( error , i ) => {
199204 if ( isPrimitive ( error ) ) {
200205 return
201206 }
202207
208+ const parsed = previous ?. errors ?. [ i ]
209+ if ( error . stack && parsed ?. stacks && parsed . stack === error . stack ) {
210+ error . stacks = parsed . stacks
211+ return
212+ }
213+
203214 const project = this . vitest . getProjectByName ( task ! . file . projectName || '' )
204215 if ( isBrowser ) {
205216 error . stacks =
@@ -209,13 +220,33 @@ export class TestRun {
209220 } else {
210221 error . stacks = parseErrorStacktrace ( error , {
211222 frameFilter : project . config . onStackTrace ,
212- getSourceMap : ( file ) => getExternalSourceMap ( project , file ) ,
223+ getSourceMap : ( file ) => this . getExternalSourceMap ( project , file ) ,
213224 } )
214225 }
215226 } )
216227 } )
217228 }
218229
230+ private getExternalSourceMap ( project : TestProject , file : string ) : unknown {
231+ // This only handles external modules since
232+ // source map is already applied for inlined modules.
233+ // Module node exists due to Vitest fetch module,
234+ // but transformResult should be empty for external modules.
235+ // TODO: this uses client module graph and thus miss detects
236+ // inlined modules in ssr environment as if external.
237+ const mod = project . vite . moduleGraph . getModuleById ( file )
238+ if ( mod ?. transformResult ) {
239+ return undefined
240+ }
241+ if ( ! this . externalSourceMaps . has ( file ) ) {
242+ const map = existsSync ( file )
243+ ? extractSourcemapFromFile ( readFileSync ( file , 'utf-8' ) , file ) ?. map
244+ : undefined
245+ this . externalSourceMaps . set ( file , map )
246+ }
247+ return this . externalSourceMaps . get ( file )
248+ }
249+
219250 private async reportEvent ( id : string , event : TaskUpdateEvent , data : TaskEventData | undefined ) {
220251 const task = this . vitest . state . idMap . get ( id )
221252 const entity = task && this . vitest . state . getReportedEntity ( task )
@@ -343,18 +374,3 @@ export class TestRun {
343374 }
344375 }
345376}
346-
347- function getExternalSourceMap ( project : TestProject , file : string ) {
348- // This only handles external modules since
349- // source map is already applied for inlined modules.
350- // Module node exists due to Vitest fetch module,
351- // but transformResult should be empty for external modules.
352- // TODO: this uses client module graph and thus miss detects
353- // inlined modules in ssr environment as if external.
354- const mod = project . vite . moduleGraph . getModuleById ( file )
355- if ( ! mod ?. transformResult && existsSync ( file ) ) {
356- const code = readFileSync ( file , 'utf-8' )
357- const result = extractSourcemapFromFile ( code , file )
358- return result ?. map
359- }
360- }
0 commit comments