[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

fix(coverage): prevent encoding filenames of uncovered files (#8239)

authored by

Ari Perkkiö and committed by
GitHub
(Jul 1, 2025, 2:48 PM +0200) 8a998804 feadc60a

+94 -4
+6 -4
packages/coverage-v8/src/provider.ts
··· 5 5 import type { AfterSuiteRunMeta } from 'vitest' 6 6 import type { CoverageProvider, ReportContext, ResolvedCoverageOptions, TestProject, Vitest } from 'vitest/node' 7 7 import { promises as fs } from 'node:fs' 8 - import { fileURLToPath, pathToFileURL } from 'node:url' 8 + import { fileURLToPath } from 'node:url' 9 9 // @ts-expect-error -- untyped 10 10 import { mergeProcessCovs } from '@bcoe/v8-coverage' 11 11 import astV8ToIstanbul from 'ast-v8-to-istanbul' ··· 173 173 timeout = setTimeout(() => debug(c.bgRed(`File "${filename}" is taking longer than 3s`)), 3_000) 174 174 } 175 175 176 - const url = pathToFileURL(filename) 176 + // Do not use pathToFileURL to avoid encoding filename parts 177 + const url = `file://${filename.startsWith('/') ? '' : '/'}${filename}` 178 + 177 179 const sources = await this.getSources( 178 - url.href, 180 + url, 179 181 transformResults, 180 182 transform, 181 183 ) 182 184 183 185 coverageMap.merge(await this.remapCoverage( 184 - url.href, 186 + url, 185 187 0, 186 188 sources, 187 189 [],
+19
test/coverage-test/test/include-exclude.test.ts
··· 332 332 ] 333 333 `) 334 334 }) 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 + })
+15
test/coverage-test/fixtures/src/tested-with-]-in-filename.ts
··· 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 + }
+47
test/coverage-test/fixtures/src/untested-with-]-in-filename.ts
··· 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 + }
+7
test/coverage-test/fixtures/test/sources-with-]-in-filenames.test.ts
··· 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 + })