Skip to content

Commit dba1337

Browse files
authoredDec 31, 2022
fix(coverage): env-replacer to remove query params from sourcemaps filenames (#2584)
* test: add failing test case for #2540 * fix(coverage): env-replacer to remove query params from sourcemaps - Fixes bugs where vue components lost coverage when import.meta.env is used
1 parent 32a577b commit dba1337

File tree

4 files changed

+117
-3
lines changed

4 files changed

+117
-3
lines changed
 

‎packages/vitest/src/node/plugins/envReplacer.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import MagicString from 'magic-string'
22
import type { Plugin } from 'vite'
33
import { stripLiteral } from 'strip-literal'
4+
import { cleanUrl } from 'vite-node/utils'
45

56
// so people can reassign envs at runtime
67
// import.meta.env.VITE_NAME = 'app' -> process.env.VITE_NAME = 'app'
@@ -27,7 +28,12 @@ export const EnvReplacerPlugin = (): Plugin => {
2728
if (s) {
2829
return {
2930
code: s.toString(),
30-
map: s.generateMap({ hires: true, source: id }),
31+
map: s.generateMap({
32+
hires: true,
33+
34+
// Remove possible query parameters, e.g. vue's "?vue&type=script&src=true&lang.ts"
35+
source: cleanUrl(id),
36+
}),
3137
}
3238
}
3339
},

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

+71-2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ exports[`c8 json report 1`] = `
7070
"0": 1,
7171
"1": 0,
7272
"2": 2,
73+
"3": 0,
7374
},
7475
"fnMap": {
7576
"0": {
@@ -144,6 +145,30 @@ exports[`c8 json report 1`] = `
144145
},
145146
"name": "coveredMethod",
146147
},
148+
"3": {
149+
"decl": {
150+
"end": {
151+
"column": 6,
152+
"line": 22,
153+
},
154+
"start": {
155+
"column": 4,
156+
"line": 20,
157+
},
158+
},
159+
"line": 20,
160+
"loc": {
161+
"end": {
162+
"column": 6,
163+
"line": 22,
164+
},
165+
"start": {
166+
"column": 4,
167+
"line": 20,
168+
},
169+
},
170+
"name": "uncoveredMethodUsingImportMeta",
171+
},
147172
},
148173
"path": "<process-cwd>/src/Counter/Counter.component.ts",
149174
"s": {
@@ -160,6 +185,10 @@ exports[`c8 json report 1`] = `
160185
"18": 1,
161186
"19": 1,
162187
"2": 1,
188+
"20": 0,
189+
"21": 0,
190+
"22": 1,
191+
"23": 1,
163192
"3": 1,
164193
"4": 1,
165194
"5": 1,
@@ -271,7 +300,7 @@ exports[`c8 json report 1`] = `
271300
},
272301
"18": {
273302
"end": {
274-
"column": 4,
303+
"column": 0,
275304
"line": 19,
276305
},
277306
"start": {
@@ -281,7 +310,7 @@ exports[`c8 json report 1`] = `
281310
},
282311
"19": {
283312
"end": {
284-
"column": 2,
313+
"column": 38,
285314
"line": 20,
286315
},
287316
"start": {
@@ -299,6 +328,46 @@ exports[`c8 json report 1`] = `
299328
"line": 3,
300329
},
301330
},
331+
"20": {
332+
"end": {
333+
"column": 94,
334+
"line": 21,
335+
},
336+
"start": {
337+
"column": 0,
338+
"line": 21,
339+
},
340+
},
341+
"21": {
342+
"end": {
343+
"column": 6,
344+
"line": 22,
345+
},
346+
"start": {
347+
"column": 0,
348+
"line": 22,
349+
},
350+
},
351+
"22": {
352+
"end": {
353+
"column": 4,
354+
"line": 23,
355+
},
356+
"start": {
357+
"column": 0,
358+
"line": 23,
359+
},
360+
},
361+
"23": {
362+
"end": {
363+
"column": 2,
364+
"line": 24,
365+
},
366+
"start": {
367+
"column": 0,
368+
"line": 24,
369+
},
370+
},
302371
"3": {
303372
"end": {
304373
"column": 18,

‎test/coverage-test/coverage-report-tests/__snapshots__/istanbul.report.test.ts.snap

+35
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ exports[`istanbul json report 1`] = `
99
"0": 1,
1010
"1": 0,
1111
"2": 2,
12+
"3": 0,
1213
},
1314
"fnMap": {
1415
"0": {
@@ -80,13 +81,37 @@ exports[`istanbul json report 1`] = `
8081
},
8182
"name": "(anonymous_2)",
8283
},
84+
"3": {
85+
"decl": {
86+
"end": {
87+
"column": 37,
88+
"line": 20,
89+
},
90+
"start": {
91+
"column": 4,
92+
"line": 20,
93+
},
94+
},
95+
"loc": {
96+
"end": {
97+
"column": null,
98+
"line": 22,
99+
},
100+
"start": {
101+
"column": 37,
102+
"line": 20,
103+
},
104+
},
105+
"name": "(anonymous_3)",
106+
},
83107
},
84108
"path": "<process-cwd>/src/Counter/Counter.component.ts",
85109
"s": {
86110
"0": 1,
87111
"1": 1,
88112
"2": 0,
89113
"3": 2,
114+
"4": 0,
90115
},
91116
"statementMap": {
92117
"0": {
@@ -129,6 +154,16 @@ exports[`istanbul json report 1`] = `
129154
"line": 17,
130155
},
131156
},
157+
"4": {
158+
"end": {
159+
"column": null,
160+
"line": 21,
161+
},
162+
"start": {
163+
"column": 6,
164+
"line": 21,
165+
},
166+
},
132167
},
133168
},
134169
"<process-cwd>/src/Counter/Counter.vue": {

‎test/coverage-test/src/Counter/Counter.component.ts

+4
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,9 @@ export default defineComponent({
1616
coveredMethod() {
1717
return 'This line should be covered'
1818
},
19+
20+
uncoveredMethodUsingImportMeta() {
21+
return `Source maps tend to break when import meta is used: ${import.meta.env.BASE_URL}`
22+
},
1923
},
2024
})

0 commit comments

Comments
 (0)
Please sign in to comment.