Skip to content

Commit

Permalink
fix(coverage): flaky c8 coverage caused by parallel sourcemap constru…
Browse files Browse the repository at this point in the history
…cting

- prevent overwriting already loaded sourcemaps by filtering out "broken" sourcemaps
  • Loading branch information
AriPerkkio committed Jan 2, 2023
1 parent e641a11 commit f3cfa58
Showing 1 changed file with 47 additions and 25 deletions.
72 changes: 47 additions & 25 deletions packages/coverage-c8/src/provider.ts
Expand Up @@ -48,39 +48,61 @@ export class C8CoverageProvider implements CoverageProvider {
takeCoverage()
const report = createReport(this.ctx.config.coverage)

interface MapAndSource { map: RawSourceMap; source: string | undefined }
type SourceMapMeta = { url: string; filepath: string } & MapAndSource

// add source maps
const sourceMapMeta: Record<string, { map: RawSourceMap; source: string | undefined }> = {}
await Promise.all(Array
const sourceMapMeta: Record<SourceMapMeta['url'], MapAndSource> = {}

const entries = Array
.from(this.ctx.vitenode.fetchCache.entries())
.filter(i => !i[0].includes('/node_modules/'))
.map(async ([file, { result }]) => {
const map = result.map
if (!map)
return
.map(([file, { result }]) => {
if (!result.map)
return null

const filepath = file.split('?')[0]

const url = _url.pathToFileURL(filepath).href

let code: string | undefined
try {
code = (await fs.readFile(filepath)).toString()
}
catch { }

// Vite does not report full path in sourcemap sources
// so use an actual file path
const sources = [url]

sourceMapMeta[url] = {
return {
filepath,
url: _url.pathToFileURL(filepath).href,
map: result.map,
source: result.code,
map: {
sourcesContent: code ? [code] : undefined,
...map,
sources,
},
}
}))
})
.filter((entry) => {
if (!entry)
return false

// Mappings and sourceContent are needed for C8 to work
return (
entry.map.mappings.length > 0
&& entry.map.sourcesContent
&& entry.map.sourcesContent.length > 0
&& entry.map.sourcesContent[0].length > 0
)
}) as SourceMapMeta[]

await Promise.all(entries.map(async ({ url, source, map, filepath }) => {
let code: string | undefined
try {
code = (await fs.readFile(filepath)).toString()
}
catch { }

// Vite does not report full path in sourcemap sources
// so use an actual file path
const sources = [url]

sourceMapMeta[url] = {
source,
map: {
sourcesContent: code ? [code] : undefined,
...map,
sources,
},
}
}))

// This is a magic number. It corresponds to the amount of code
// that we add in packages/vite-node/src/client.ts:114 (vm.runInThisContext)
Expand Down

0 comments on commit f3cfa58

Please sign in to comment.