Skip to content

Commit 1f6cb59

Browse files
fix(coverage): v8 to support source maps with multiple sources (#6120)
Co-authored-by: Vladimir <sleuths.slews0s@icloud.com>
1 parent e662c7b commit 1f6cb59

File tree

13 files changed

+763
-146
lines changed

13 files changed

+763
-146
lines changed

package.json

+1-1
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

+17-12
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)