From dba13374aa1cd93efd9f785821b6d82335ca7ec2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Sat, 31 Dec 2022 14:51:55 +0200 Subject: [PATCH] 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 --- .../vitest/src/node/plugins/envReplacer.ts | 8 +- .../__snapshots__/c8.report.test.ts.snap | 73 ++++++++++++++++++- .../istanbul.report.test.ts.snap | 35 +++++++++ .../src/Counter/Counter.component.ts | 4 + 4 files changed, 117 insertions(+), 3 deletions(-) diff --git a/packages/vitest/src/node/plugins/envReplacer.ts b/packages/vitest/src/node/plugins/envReplacer.ts index 29a4b1b22a3..3cffb3a9bee 100644 --- a/packages/vitest/src/node/plugins/envReplacer.ts +++ b/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' @@ -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), + }), } } }, diff --git a/test/coverage-test/coverage-report-tests/__snapshots__/c8.report.test.ts.snap b/test/coverage-test/coverage-report-tests/__snapshots__/c8.report.test.ts.snap index bf75cc04c8a..07f8eb6edfc 100644 --- a/test/coverage-test/coverage-report-tests/__snapshots__/c8.report.test.ts.snap +++ b/test/coverage-test/coverage-report-tests/__snapshots__/c8.report.test.ts.snap @@ -70,6 +70,7 @@ exports[`c8 json report 1`] = ` "0": 1, "1": 0, "2": 2, + "3": 0, }, "fnMap": { "0": { @@ -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": "/src/Counter/Counter.component.ts", "s": { @@ -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, @@ -271,7 +300,7 @@ exports[`c8 json report 1`] = ` }, "18": { "end": { - "column": 4, + "column": 0, "line": 19, }, "start": { @@ -281,7 +310,7 @@ exports[`c8 json report 1`] = ` }, "19": { "end": { - "column": 2, + "column": 38, "line": 20, }, "start": { @@ -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, diff --git a/test/coverage-test/coverage-report-tests/__snapshots__/istanbul.report.test.ts.snap b/test/coverage-test/coverage-report-tests/__snapshots__/istanbul.report.test.ts.snap index 721462d2ba8..1a268c4af53 100644 --- a/test/coverage-test/coverage-report-tests/__snapshots__/istanbul.report.test.ts.snap +++ b/test/coverage-test/coverage-report-tests/__snapshots__/istanbul.report.test.ts.snap @@ -9,6 +9,7 @@ exports[`istanbul json report 1`] = ` "0": 1, "1": 0, "2": 2, + "3": 0, }, "fnMap": { "0": { @@ -80,6 +81,29 @@ 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": "/src/Counter/Counter.component.ts", "s": { @@ -87,6 +111,7 @@ exports[`istanbul json report 1`] = ` "1": 1, "2": 0, "3": 2, + "4": 0, }, "statementMap": { "0": { @@ -129,6 +154,16 @@ exports[`istanbul json report 1`] = ` "line": 17, }, }, + "4": { + "end": { + "column": null, + "line": 21, + }, + "start": { + "column": 6, + "line": 21, + }, + }, }, }, "/src/Counter/Counter.vue": { diff --git a/test/coverage-test/src/Counter/Counter.component.ts b/test/coverage-test/src/Counter/Counter.component.ts index 372a62ad459..fb1e1f16566 100644 --- a/test/coverage-test/src/Counter/Counter.component.ts +++ b/test/coverage-test/src/Counter/Counter.component.ts @@ -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}` + }, }, })