Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(coverage): v8 to support source maps with multiple sources #6120

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed for vitejs/vite#17677

"vitest": "workspace:*",
"zx": "^8.1.4"
},
Expand Down
29 changes: 17 additions & 12 deletions packages/coverage-v8/src/provider.ts
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified that all our old tests pass with these changes when used with older Vite versions. ✅

Original file line number Diff line number Diff line change
Expand Up @@ -457,39 +457,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