@@ -468,39 +468,44 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage
468468
469469 const map = transformResult ?. map as EncodedSourceMap | undefined
470470 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 ( ( ) => {
474475 // If file does not exist construct a dummy source for it.
475476 // These can be files that were generated dynamically during the test run and were removed after it.
476477 const length = findLongestFunctionLength ( functions )
477478 return '.' . repeat ( length )
478- } ) )
479+ } )
480+ }
479481
480482 // These can be uncovered files included by "all: true" or files that are loaded outside vite-node
481483 if ( ! map ) {
482484 return {
483485 isExecuted,
484- source : code || sourcesContent ,
485- originalSource : sourcesContent ,
486+ source : code || sourcesContent [ 0 ] ,
487+ originalSource : sourcesContent [ 0 ] ,
486488 }
487489 }
488490
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 )
492497 }
493498
494499 return {
495500 isExecuted,
496- originalSource : sourcesContent ,
497- source : code || sourcesContent ,
501+ originalSource : sourcesContent [ 0 ] ,
502+ source : code || sourcesContent [ 0 ] ,
498503 sourceMap : {
499504 sourcemap : excludeGeneratedCode ( code , {
500505 ...map ,
501506 version : 3 ,
502507 sources,
503- sourcesContent : [ sourcesContent ] ,
508+ sourcesContent,
504509 } ) ,
505510 } ,
506511 }
0 commit comments