Skip to content

Commit

Permalink
fix(coverage): v8 to support source maps with multiple sources (#6120)
Browse files Browse the repository at this point in the history
Co-authored-by: Vladimir <[email protected]>
  • Loading branch information
AriPerkkio and sheremet-va authored Aug 12, 2024
1 parent e662c7b commit 1f6cb59
Show file tree
Hide file tree
Showing 13 changed files with 763 additions and 146 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"rollup-plugin-license": "^3.5.2",
"tsx": "^4.16.5",
"typescript": "^5.5.4",
"vite": "^5.3.3",
"vite": "^5.4.0",
"vitest": "workspace:*",
"zx": "^8.1.4"
},
Expand Down
29 changes: 17 additions & 12 deletions packages/coverage-v8/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,39 +468,44 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage

const map = transformResult?.map as EncodedSourceMap | undefined
const code = transformResult?.code
const sourcesContent
= map?.sourcesContent?.[0]
|| (await fs.readFile(filePath, 'utf-8').catch(() => {
const sourcesContent = map?.sourcesContent || []

if (!sourcesContent[0]) {
sourcesContent[0] = await fs.readFile(filePath, 'utf-8').catch(() => {
// If file does not exist construct a dummy source for it.
// These can be files that were generated dynamically during the test run and were removed after it.
const length = findLongestFunctionLength(functions)
return '.'.repeat(length)
}))
})
}

// These can be uncovered files included by "all: true" or files that are loaded outside vite-node
if (!map) {
return {
isExecuted,
source: code || sourcesContent,
originalSource: sourcesContent,
source: code || sourcesContent[0],
originalSource: sourcesContent[0],
}
}

const sources = [url]
if (map.sources && map.sources[0] && !url.endsWith(map.sources[0])) {
sources[0] = new URL(map.sources[0], url).href
const sources = (map.sources || [])
.filter(source => source != null)
.map(source => new URL(source, url).href)

if (sources.length === 0) {
sources.push(url)
}

return {
isExecuted,
originalSource: sourcesContent,
source: code || sourcesContent,
originalSource: sourcesContent[0],
source: code || sourcesContent[0],
sourceMap: {
sourcemap: excludeGeneratedCode(code, {
...map,
version: 3,
sources,
sourcesContent: [sourcesContent],
sourcesContent,
}),
},
}
Expand Down
Loading

0 comments on commit 1f6cb59

Please sign in to comment.