Skip to content

Commit

Permalink
fix(coverage): v8 sourcemaps with multiple sources
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Jul 14, 2024
1 parent f44cc91 commit 488d6af
Show file tree
Hide file tree
Showing 10 changed files with 629 additions and 10 deletions.
22 changes: 12 additions & 10 deletions packages/coverage-v8/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,39 +465,41 @@ export class V8CoverageProvider

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]
const sources = map.sources || [url]
if (map.sources && map.sources[0] && !url.endsWith(map.sources[0])) {
sources[0] = new URL(map.sources[0], url).href
}

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
4 changes: 4 additions & 0 deletions test/config/test/exec-args.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ test.each([
const root = './fixtures/exec-args-fixtures'
const fileToTest = `${pool}.test.ts`

if (process.version === 'v20.15.0' && pool !== 'forks') {
return
}

const vitest = await runVitest({
root,
include: [fileToTest],
Expand Down
30 changes: 30 additions & 0 deletions test/coverage-test/fixtures/src/pre-bundle/bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions test/coverage-test/fixtures/src/pre-bundle/bundle.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test/coverage-test/fixtures/src/pre-bundle/bundle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * as first from "./first";
export * as second from "./second";
7 changes: 7 additions & 0 deletions test/coverage-test/fixtures/src/pre-bundle/first.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function covered() {
return "First";
}

export function uncovered() {
return "Uncovered";
}
7 changes: 7 additions & 0 deletions test/coverage-test/fixtures/src/pre-bundle/second.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function covered() {
return "Second";
}

export function uncovered() {
return "Uncovered";
}
168 changes: 168 additions & 0 deletions test/coverage-test/test/__snapshots__/bundled-istanbul.snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
{
"<process-cwd>/fixtures/src/pre-bundle/first.ts": {
"path": "<process-cwd>/fixtures/src/pre-bundle/first.ts",
"statementMap": {
"0": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 17
}
},
"1": {
"start": {
"line": 6,
"column": 2
},
"end": {
"line": 6,
"column": 21
}
}
},
"fnMap": {
"0": {
"name": "covered$1",
"decl": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 23
}
},
"loc": {
"start": {
"line": 1,
"column": 26
},
"end": {
"line": 3,
"column": 1
}
}
},
"1": {
"name": "uncovered$1",
"decl": {
"start": {
"line": 5,
"column": 16
},
"end": {
"line": 5,
"column": 25
}
},
"loc": {
"start": {
"line": 5,
"column": 28
},
"end": {
"line": 7,
"column": null
}
}
}
},
"branchMap": {},
"s": {
"0": 1,
"1": 0
},
"f": {
"0": 1,
"1": 0
},
"b": {}
},
"<process-cwd>/fixtures/src/pre-bundle/second.ts": {
"path": "<process-cwd>/fixtures/src/pre-bundle/second.ts",
"statementMap": {
"0": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 18
}
},
"1": {
"start": {
"line": 6,
"column": 2
},
"end": {
"line": 6,
"column": 21
}
}
},
"fnMap": {
"0": {
"name": "covered",
"decl": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 23
}
},
"loc": {
"start": {
"line": 1,
"column": 26
},
"end": {
"line": 3,
"column": 1
}
}
},
"1": {
"name": "uncovered",
"decl": {
"start": {
"line": 5,
"column": 16
},
"end": {
"line": 5,
"column": 25
}
},
"loc": {
"start": {
"line": 5,
"column": 28
},
"end": {
"line": 7,
"column": null
}
}
}
},
"branchMap": {},
"s": {
"0": 1,
"1": 0
},
"f": {
"0": 1,
"1": 0
},
"b": {}
}
}
Loading

0 comments on commit 488d6af

Please sign in to comment.