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 (#2591)

Co-authored-by: Vladimir <sleuths.slews0s@icloud.com>
Fixes undefined
  • Loading branch information
AriPerkkio authored Jan 6, 2023
1 parent f8ac209 commit 9a29f98
Show file tree
Hide file tree
Showing 4 changed files with 319 additions and 36 deletions.
72 changes: 47 additions & 25 deletions packages/coverage-c8/src/provider.ts
Original file line number Diff line number Diff line change
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 sourcesContent 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
Original file line number Diff line number Diff line change
Expand Up @@ -809,19 +809,63 @@ exports[`c8 json report 1`] = `
},
"<process-cwd>/src/Defined.vue": {
"all": false,
"b": {},
"branchMap": {},
"b": {
"0": [
0,
],
},
"branchMap": {
"0": {
"line": 6,
"loc": {
"end": {
"column": 36,
"line": 8,
},
"start": {
"column": 33,
"line": 6,
},
},
"locations": [
{
"end": {
"column": 36,
"line": 8,
},
"start": {
"column": 33,
"line": 6,
},
},
],
"type": "branch",
},
},
"f": {},
"fnMap": {},
"path": "<process-cwd>/src/Defined.vue",
"s": {
"0": 1,
"1": 1,
"10": 1,
"11": 1,
"12": 1,
"13": 1,
"14": 1,
"15": 1,
"16": 1,
"17": 1,
"18": 1,
"19": 1,
"2": 1,
"3": 1,
"4": 1,
"5": 1,
"6": 1,
"6": 0,
"7": 0,
"8": 1,
"9": 1,
},
"statementMap": {
"0": {
Expand All @@ -844,9 +888,109 @@ exports[`c8 json report 1`] = `
"line": 2,
},
},
"10": {
"end": {
"column": 10,
"line": 11,
},
"start": {
"column": 0,
"line": 11,
},
},
"11": {
"end": {
"column": 15,
"line": 12,
},
"start": {
"column": 0,
"line": 12,
},
},
"12": {
"end": {
"column": 11,
"line": 13,
},
"start": {
"column": 0,
"line": 13,
},
},
"13": {
"end": {
"column": 0,
"line": 14,
},
"start": {
"column": 0,
"line": 14,
},
},
"14": {
"end": {
"column": 90,
"line": 15,
},
"start": {
"column": 0,
"line": 15,
},
},
"15": {
"end": {
"column": 25,
"line": 16,
},
"start": {
"column": 0,
"line": 16,
},
},
"16": {
"end": {
"column": 6,
"line": 17,
},
"start": {
"column": 0,
"line": 17,
},
},
"17": {
"end": {
"column": 25,
"line": 18,
},
"start": {
"column": 0,
"line": 18,
},
},
"18": {
"end": {
"column": 1,
"line": 19,
},
"start": {
"column": 0,
"line": 19,
},
},
"19": {
"end": {
"column": 8,
"line": 20,
},
"start": {
"column": 0,
"line": 20,
},
},
"2": {
"end": {
"column": 9,
"column": 0,
"line": 3,
},
"start": {
Expand All @@ -856,7 +1000,7 @@ exports[`c8 json report 1`] = `
},
"3": {
"end": {
"column": 0,
"column": 31,
"line": 4,
},
"start": {
Expand All @@ -866,7 +1010,7 @@ exports[`c8 json report 1`] = `
},
"4": {
"end": {
"column": 10,
"column": 12,
"line": 5,
},
"start": {
Expand All @@ -876,7 +1020,7 @@ exports[`c8 json report 1`] = `
},
"5": {
"end": {
"column": 15,
"column": 34,
"line": 6,
},
"start": {
Expand All @@ -886,14 +1030,44 @@ exports[`c8 json report 1`] = `
},
"6": {
"end": {
"column": 11,
"column": 4,
"line": 7,
},
"start": {
"column": 0,
"line": 7,
},
},
"7": {
"end": {
"column": 36,
"line": 8,
},
"start": {
"column": 0,
"line": 8,
},
},
"8": {
"end": {
"column": 9,
"line": 9,
},
"start": {
"column": 0,
"line": 9,
},
},
"9": {
"end": {
"column": 0,
"line": 10,
},
"start": {
"column": 0,
"line": 10,
},
},
},
},
"<process-cwd>/src/Hello.vue": {
Expand Down
Loading

0 comments on commit 9a29f98

Please sign in to comment.