Skip to content

Commit 8a99880

Browse files
authored
fix(coverage): prevent encoding filenames of uncovered files (#8239)
1 parent feadc60 commit 8a99880

File tree

5 files changed

+94
-4
lines changed

5 files changed

+94
-4
lines changed

packages/coverage-v8/src/provider.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { EncodedSourceMap, FetchResult } from 'vite-node'
55
import type { AfterSuiteRunMeta } from 'vitest'
66
import type { CoverageProvider, ReportContext, ResolvedCoverageOptions, TestProject, Vitest } from 'vitest/node'
77
import { promises as fs } from 'node:fs'
8-
import { fileURLToPath, pathToFileURL } from 'node:url'
8+
import { fileURLToPath } from 'node:url'
99
// @ts-expect-error -- untyped
1010
import { mergeProcessCovs } from '@bcoe/v8-coverage'
1111
import astV8ToIstanbul from 'ast-v8-to-istanbul'
@@ -173,15 +173,17 @@ export class V8CoverageProvider extends BaseCoverageProvider<ResolvedCoverageOpt
173173
timeout = setTimeout(() => debug(c.bgRed(`File "${filename}" is taking longer than 3s`)), 3_000)
174174
}
175175

176-
const url = pathToFileURL(filename)
176+
// Do not use pathToFileURL to avoid encoding filename parts
177+
const url = `file://${filename.startsWith('/') ? '' : '/'}${filename}`
178+
177179
const sources = await this.getSources(
178-
url.href,
180+
url,
179181
transformResults,
180182
transform,
181183
)
182184

183185
coverageMap.merge(await this.remapCoverage(
184-
url.href,
186+
url,
185187
0,
186188
sources,
187189
[],
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default function covered(a: number, b: number) {
2+
if(a === 2 && b === 3) {
3+
return 5
4+
}
5+
6+
return a + b
7+
}
8+
9+
export function uncovered(a: number, b: number) {
10+
if(a === 2 && b === 3) {
11+
return 5
12+
}
13+
14+
return a + b
15+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Some top level comment which adds some padding. This helps us see
3+
* if sourcemaps are off.
4+
*/
5+
6+
/* eslint-disable unused-imports/no-unused-vars -- intentional */
7+
8+
export default function untestedFile() {
9+
return 'Untetsted'
10+
}
11+
12+
function add(a: number, b: number) {
13+
// This line should NOT be covered
14+
return a + b
15+
}
16+
17+
type TypescriptTypings = 1 | 2
18+
19+
function multiply(a: number, b: number) {
20+
// This line should NOT be covered
21+
return a * b
22+
}
23+
24+
export function math(a: number, b: number, operator: '*' | '+') {
25+
interface MoreCompileTimeCode {
26+
should: {
27+
be: {
28+
excluded: true
29+
}
30+
}
31+
}
32+
33+
if (operator === '*') {
34+
// This line should NOT be covered
35+
return multiply(a, b)
36+
}
37+
38+
/* v8 ignore start */
39+
if (operator === '+') {
40+
// This line should be excluded
41+
return add(a, b)
42+
}
43+
/* v8 ignore stop */
44+
45+
// This line should NOT be covered
46+
throw new Error('Unsupported operator')
47+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { expect, test } from "vitest"
2+
3+
test("cover lines", async () => {
4+
const mod = await import("../src/tested-with-]-in-filename")
5+
6+
expect(mod.default(100, 2)).toBe(102)
7+
})

test/coverage-test/test/include-exclude.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,22 @@ test('files included and excluded in project\'s plugin\'s configureVitest are ex
332332
]
333333
`)
334334
})
335+
336+
test('includes covered and uncovered with ] in filenames', async () => {
337+
await runVitest({
338+
include: ['fixtures/test/sources-with-]-in-filenames.test.ts'],
339+
coverage: {
340+
reporter: 'json',
341+
include: ['**/untested-with-*', '**/tested-with-*'],
342+
343+
},
344+
})
345+
346+
const coverageMap = await readCoverageMap()
347+
expect(coverageMap.files()).toMatchInlineSnapshot(`
348+
[
349+
"<process-cwd>/fixtures/src/tested-with-]-in-filename.ts",
350+
"<process-cwd>/fixtures/src/untested-with-]-in-filename.ts",
351+
]
352+
`)
353+
})

0 commit comments

Comments
 (0)