Skip to content

Commit

Permalink
fix(coverage): env-replacer to remove query params from sourcemaps fi…
Browse files Browse the repository at this point in the history
…lenames (#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
  • Loading branch information
AriPerkkio committed Dec 31, 2022
1 parent 32a577b commit dba1337
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 3 deletions.
8 changes: 7 additions & 1 deletion packages/vitest/src/node/plugins/envReplacer.ts
@@ -1,6 +1,7 @@
import MagicString from 'magic-string'
import type { Plugin } from 'vite'
import { stripLiteral } from 'strip-literal'
import { cleanUrl } from 'vite-node/utils'

// so people can reassign envs at runtime
// import.meta.env.VITE_NAME = 'app' -> process.env.VITE_NAME = 'app'
Expand All @@ -27,7 +28,12 @@ export const EnvReplacerPlugin = (): Plugin => {
if (s) {
return {
code: s.toString(),
map: s.generateMap({ hires: true, source: id }),
map: s.generateMap({
hires: true,

// Remove possible query parameters, e.g. vue's "?vue&type=script&src=true&lang.ts"
source: cleanUrl(id),
}),
}
}
},
Expand Down
Expand Up @@ -70,6 +70,7 @@ exports[`c8 json report 1`] = `
"0": 1,
"1": 0,
"2": 2,
"3": 0,
},
"fnMap": {
"0": {
Expand Down Expand Up @@ -144,6 +145,30 @@ exports[`c8 json report 1`] = `
},
"name": "coveredMethod",
},
"3": {
"decl": {
"end": {
"column": 6,
"line": 22,
},
"start": {
"column": 4,
"line": 20,
},
},
"line": 20,
"loc": {
"end": {
"column": 6,
"line": 22,
},
"start": {
"column": 4,
"line": 20,
},
},
"name": "uncoveredMethodUsingImportMeta",
},
},
"path": "<process-cwd>/src/Counter/Counter.component.ts",
"s": {
Expand All @@ -160,6 +185,10 @@ exports[`c8 json report 1`] = `
"18": 1,
"19": 1,
"2": 1,
"20": 0,
"21": 0,
"22": 1,
"23": 1,
"3": 1,
"4": 1,
"5": 1,
Expand Down Expand Up @@ -271,7 +300,7 @@ exports[`c8 json report 1`] = `
},
"18": {
"end": {
"column": 4,
"column": 0,
"line": 19,
},
"start": {
Expand All @@ -281,7 +310,7 @@ exports[`c8 json report 1`] = `
},
"19": {
"end": {
"column": 2,
"column": 38,
"line": 20,
},
"start": {
Expand All @@ -299,6 +328,46 @@ exports[`c8 json report 1`] = `
"line": 3,
},
},
"20": {
"end": {
"column": 94,
"line": 21,
},
"start": {
"column": 0,
"line": 21,
},
},
"21": {
"end": {
"column": 6,
"line": 22,
},
"start": {
"column": 0,
"line": 22,
},
},
"22": {
"end": {
"column": 4,
"line": 23,
},
"start": {
"column": 0,
"line": 23,
},
},
"23": {
"end": {
"column": 2,
"line": 24,
},
"start": {
"column": 0,
"line": 24,
},
},
"3": {
"end": {
"column": 18,
Expand Down
Expand Up @@ -9,6 +9,7 @@ exports[`istanbul json report 1`] = `
"0": 1,
"1": 0,
"2": 2,
"3": 0,
},
"fnMap": {
"0": {
Expand Down Expand Up @@ -80,13 +81,37 @@ exports[`istanbul json report 1`] = `
},
"name": "(anonymous_2)",
},
"3": {
"decl": {
"end": {
"column": 37,
"line": 20,
},
"start": {
"column": 4,
"line": 20,
},
},
"loc": {
"end": {
"column": null,
"line": 22,
},
"start": {
"column": 37,
"line": 20,
},
},
"name": "(anonymous_3)",
},
},
"path": "<process-cwd>/src/Counter/Counter.component.ts",
"s": {
"0": 1,
"1": 1,
"2": 0,
"3": 2,
"4": 0,
},
"statementMap": {
"0": {
Expand Down Expand Up @@ -129,6 +154,16 @@ exports[`istanbul json report 1`] = `
"line": 17,
},
},
"4": {
"end": {
"column": null,
"line": 21,
},
"start": {
"column": 6,
"line": 21,
},
},
},
},
"<process-cwd>/src/Counter/Counter.vue": {
Expand Down
4 changes: 4 additions & 0 deletions test/coverage-test/src/Counter/Counter.component.ts
Expand Up @@ -16,5 +16,9 @@ export default defineComponent({
coveredMethod() {
return 'This line should be covered'
},

uncoveredMethodUsingImportMeta() {
return `Source maps tend to break when import meta is used: ${import.meta.env.BASE_URL}`
},
},
})

0 comments on commit dba1337

Please sign in to comment.