@@ -110,7 +110,7 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage
110110 }
111111
112112 const converted = await Promise . all ( scriptCoverages . map ( async ( { url, functions } ) => {
113- const sources = await this . getSources ( url )
113+ const sources = await this . getSources ( url , functions )
114114
115115 // If no source map was found from vite-node we can assume this file was not run in the wrapper
116116 const wrapperLength = sources . sourceMap ? WRAPPER_LENGTH : 0
@@ -204,7 +204,7 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage
204204 } ) )
205205 }
206206
207- private async getSources ( url : string ) : Promise < {
207+ private async getSources ( url : string , functions : Profiler . FunctionCoverage [ ] = [ ] ) : Promise < {
208208 source : string
209209 originalSource ?: string
210210 sourceMap ?: { sourcemap : EncodedSourceMap }
@@ -217,7 +217,12 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage
217217
218218 const map = transformResult ?. map
219219 const code = transformResult ?. code
220- const sourcesContent = map ?. sourcesContent ?. [ 0 ] || await fs . readFile ( filePath , 'utf-8' )
220+ const sourcesContent = map ?. sourcesContent ?. [ 0 ] || await fs . readFile ( filePath , 'utf-8' ) . catch ( ( ) => {
221+ // If file does not exist construct a dummy source for it.
222+ // These can be files that were generated dynamically during the test run and were removed after it.
223+ const length = findLongestFunctionLength ( functions )
224+ return '.' . repeat ( length )
225+ } )
221226
222227 // These can be uncovered files included by "all: true" or files that are loaded outside vite-node
223228 if ( ! map )
@@ -261,3 +266,14 @@ function removeViteHelpersFromSourceMaps(source: string | undefined, map: Encode
261266
262267 return combinedMap as EncodedSourceMap
263268}
269+
270+ /**
271+ * Find the function with highest `endOffset` to determine the length of the file
272+ */
273+ function findLongestFunctionLength ( functions : Profiler . FunctionCoverage [ ] ) {
274+ return functions . reduce ( ( previous , current ) => {
275+ const maxEndOffset = current . ranges . reduce ( ( endOffset , range ) => Math . max ( endOffset , range . endOffset ) , 0 )
276+
277+ return Math . max ( previous , maxEndOffset )
278+ } , 0 )
279+ }
0 commit comments