Skip to content

Commit c85edb1

Browse files
authored
fix(coverage): esbuild to preserve legal comments for ignore hints (#2496)
1 parent 0a87ebb commit c85edb1

File tree

5 files changed

+130
-0
lines changed

5 files changed

+130
-0
lines changed

packages/vitest/src/node/plugins/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest('t
9292
const config: ViteConfig = {
9393
esbuild: {
9494
sourcemap: 'external',
95+
96+
// Enables using ignore hint for coverage providers with @preserve keyword
97+
legalComments: 'inline',
9598
},
9699
resolve: {
97100
// by default Vite resolves `module` field, which not always a native ESM module

test/coverage-test/coverage-report-tests/__snapshots__/c8.report.test.ts.snap

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2272,6 +2272,8 @@ exports[`c8 json report 1`] = `
22722272
"f": {
22732273
"0": 1,
22742274
"1": 1,
2275+
"10": 0,
2276+
"11": 0,
22752277
"2": 2,
22762278
"3": 2,
22772279
"4": 0,
@@ -2330,6 +2332,54 @@ exports[`c8 json report 1`] = `
23302332
},
23312333
"name": "get",
23322334
},
2335+
"10": {
2336+
"decl": {
2337+
"end": {
2338+
"column": 1,
2339+
"line": 29,
2340+
},
2341+
"start": {
2342+
"column": 7,
2343+
"line": 27,
2344+
},
2345+
},
2346+
"line": 27,
2347+
"loc": {
2348+
"end": {
2349+
"column": 1,
2350+
"line": 29,
2351+
},
2352+
"start": {
2353+
"column": 7,
2354+
"line": 27,
2355+
},
2356+
},
2357+
"name": "ignoredFunction",
2358+
},
2359+
"11": {
2360+
"decl": {
2361+
"end": {
2362+
"column": 1,
2363+
"line": 29,
2364+
},
2365+
"start": {
2366+
"column": 0,
2367+
"line": 29,
2368+
},
2369+
},
2370+
"line": 29,
2371+
"loc": {
2372+
"end": {
2373+
"column": 1,
2374+
"line": 29,
2375+
},
2376+
"start": {
2377+
"column": 0,
2378+
"line": 29,
2379+
},
2380+
},
2381+
"name": "get",
2382+
},
23332383
"2": {
23342384
"decl": {
23352385
"end": {
@@ -2542,6 +2592,11 @@ exports[`c8 json report 1`] = `
25422592
"21": 0,
25432593
"22": 0,
25442594
"23": 0,
2595+
"24": 1,
2596+
"25": 1,
2597+
"26": 1,
2598+
"27": 0,
2599+
"28": 0,
25452600
"3": 1,
25462601
"4": 1,
25472602
"5": 2,
@@ -2721,6 +2776,56 @@ exports[`c8 json report 1`] = `
27212776
"line": 24,
27222777
},
27232778
},
2779+
"24": {
2780+
"end": {
2781+
"column": 0,
2782+
"line": 25,
2783+
},
2784+
"start": {
2785+
"column": 0,
2786+
"line": 25,
2787+
},
2788+
},
2789+
"25": {
2790+
"end": {
2791+
"column": 39,
2792+
"line": 26,
2793+
},
2794+
"start": {
2795+
"column": 0,
2796+
"line": 26,
2797+
},
2798+
},
2799+
"26": {
2800+
"end": {
2801+
"column": 35,
2802+
"line": 27,
2803+
},
2804+
"start": {
2805+
"column": 0,
2806+
"line": 27,
2807+
},
2808+
},
2809+
"27": {
2810+
"end": {
2811+
"column": 59,
2812+
"line": 28,
2813+
},
2814+
"start": {
2815+
"column": 0,
2816+
"line": 28,
2817+
},
2818+
},
2819+
"28": {
2820+
"end": {
2821+
"column": 1,
2822+
"line": 29,
2823+
},
2824+
"start": {
2825+
"column": 0,
2826+
"line": 29,
2827+
},
2828+
},
27242829
"3": {
27252830
"end": {
27262831
"column": 0,

test/coverage-test/coverage-report-tests/istanbul.report.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,19 @@ test('implicit else is included in branch count', async () => {
2121
expect(fileCoverage.b).toHaveProperty('0')
2222
expect(fileCoverage.b['0']).toHaveLength(2)
2323
})
24+
25+
test('ignored code is excluded from the report', async () => {
26+
const functionName = 'ignoredFunction'
27+
const filename = '<process-cwd>/src/utils.ts'
28+
29+
const coverageMap = await readCoverageJson()
30+
const fileCoverage = coverageMap[filename]
31+
32+
// Function should not be included in report
33+
const functionCoverage = Object.values(fileCoverage.fnMap).find(fn => fn.name === functionName)
34+
expect(functionCoverage).toBe(undefined)
35+
36+
// Function should still be found from the actual sources
37+
const utils = await import('../src/utils')
38+
expect(utils[functionName]).toBeTypeOf('function')
39+
})

test/coverage-test/coverage-report-tests/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ interface CoverageFinalJson {
55
[filename: string]: {
66
path: string
77
b: Record<string, number[]>
8+
fnMap: Record<string, { name: string }>
89
// ... and more unrelated keys
910
}
1011
}

test/coverage-test/src/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,8 @@ export function run() {
2222
// this should not be covered
2323
divide(1, 1)
2424
}
25+
26+
/* istanbul ignore next -- @preserve */
27+
export function ignoredFunction() {
28+
return 'This function is excluded from istanbul coverage'
29+
}

0 commit comments

Comments
 (0)