@@ -468,39 +468,44 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage
468
468
469
469
const map = transformResult ?. map as EncodedSourceMap | undefined
470
470
const code = transformResult ?. code
471
- const sourcesContent
472
- = map ?. sourcesContent ?. [ 0 ]
473
- || ( await fs . readFile ( filePath , 'utf-8' ) . catch ( ( ) => {
471
+ const sourcesContent = map ?. sourcesContent || [ ]
472
+
473
+ if ( ! sourcesContent [ 0 ] ) {
474
+ sourcesContent [ 0 ] = await fs . readFile ( filePath , 'utf-8' ) . catch ( ( ) => {
474
475
// If file does not exist construct a dummy source for it.
475
476
// These can be files that were generated dynamically during the test run and were removed after it.
476
477
const length = findLongestFunctionLength ( functions )
477
478
return '.' . repeat ( length )
478
- } ) )
479
+ } )
480
+ }
479
481
480
482
// These can be uncovered files included by "all: true" or files that are loaded outside vite-node
481
483
if ( ! map ) {
482
484
return {
483
485
isExecuted,
484
- source : code || sourcesContent ,
485
- originalSource : sourcesContent ,
486
+ source : code || sourcesContent [ 0 ] ,
487
+ originalSource : sourcesContent [ 0 ] ,
486
488
}
487
489
}
488
490
489
- const sources = [ url ]
490
- if ( map . sources && map . sources [ 0 ] && ! url . endsWith ( map . sources [ 0 ] ) ) {
491
- sources [ 0 ] = new URL ( map . sources [ 0 ] , url ) . href
491
+ const sources = ( map . sources || [ ] )
492
+ . filter ( source => source != null )
493
+ . map ( source => new URL ( source , url ) . href )
494
+
495
+ if ( sources . length === 0 ) {
496
+ sources . push ( url )
492
497
}
493
498
494
499
return {
495
500
isExecuted,
496
- originalSource : sourcesContent ,
497
- source : code || sourcesContent ,
501
+ originalSource : sourcesContent [ 0 ] ,
502
+ source : code || sourcesContent [ 0 ] ,
498
503
sourceMap : {
499
504
sourcemap : excludeGeneratedCode ( code , {
500
505
...map ,
501
506
version : 3 ,
502
507
sources,
503
- sourcesContent : [ sourcesContent ] ,
508
+ sourcesContent,
504
509
} ) ,
505
510
} ,
506
511
}
0 commit comments