Skip to content

Commit 9f6ae36

Browse files
committed
fix(coverage): v8 sourcemaps with multiple sources
1 parent 7f0ae29 commit 9f6ae36

File tree

13 files changed

+758
-144
lines changed

13 files changed

+758
-144
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"rollup-plugin-license": "^3.5.2",
6262
"tsx": "^4.16.5",
6363
"typescript": "^5.5.4",
64-
"vite": "^5.3.3",
64+
"vite": "^5.4.0",
6565
"vitest": "workspace:*",
6666
"zx": "^8.1.4"
6767
},

packages/coverage-v8/src/provider.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -457,39 +457,41 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage
457457

458458
const map = transformResult?.map as EncodedSourceMap | undefined
459459
const code = transformResult?.code
460-
const sourcesContent
461-
= map?.sourcesContent?.[0]
462-
|| (await fs.readFile(filePath, 'utf-8').catch(() => {
460+
const sourcesContent = map?.sourcesContent || []
461+
462+
if (!sourcesContent[0]) {
463+
sourcesContent[0] = await fs.readFile(filePath, 'utf-8').catch(() => {
463464
// If file does not exist construct a dummy source for it.
464465
// These can be files that were generated dynamically during the test run and were removed after it.
465466
const length = findLongestFunctionLength(functions)
466467
return '.'.repeat(length)
467-
}))
468+
})
469+
}
468470

469471
// These can be uncovered files included by "all: true" or files that are loaded outside vite-node
470472
if (!map) {
471473
return {
472474
isExecuted,
473-
source: code || sourcesContent,
474-
originalSource: sourcesContent,
475+
source: code || sourcesContent[0],
476+
originalSource: sourcesContent[0],
475477
}
476478
}
477479

478-
const sources = [url]
480+
const sources = map.sources || [url]
479481
if (map.sources && map.sources[0] && !url.endsWith(map.sources[0])) {
480482
sources[0] = new URL(map.sources[0], url).href
481483
}
482484

483485
return {
484486
isExecuted,
485-
originalSource: sourcesContent,
486-
source: code || sourcesContent,
487+
originalSource: sourcesContent[0],
488+
source: code || sourcesContent[0],
487489
sourceMap: {
488490
sourcemap: excludeGeneratedCode(code, {
489491
...map,
490492
version: 3,
491493
sources,
492-
sourcesContent: [sourcesContent],
494+
sourcesContent,
493495
}),
494496
},
495497
}

0 commit comments

Comments
 (0)