-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(coverage): handle query param based transforms correctly #8418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sheremet-va
merged 3 commits into
vitest-dev:main
from
AriPerkkio:fix/v8-coverage-query-params
Aug 15, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
test/coverage-test/fixtures/configs/vitest.config.query-param-transform.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { defineConfig, mergeConfig } from 'vitest/config' | ||
| import MagicString from 'magic-string' | ||
| import remapping from '@jridgewell/remapping' | ||
| import type { Plugin } from 'vite' | ||
|
|
||
| import base from './vitest.config' | ||
|
|
||
| export default mergeConfig( | ||
| base, | ||
| defineConfig({ | ||
| plugins: [QueryParamTransforms()], | ||
| test: {} | ||
| }) | ||
| ) | ||
|
|
||
| /** | ||
| * Attempts to do Vue-like query param based transforms | ||
| */ | ||
| function QueryParamTransforms(): Plugin { | ||
| return { | ||
| name: 'vitest-custom-query-param-based-transform', | ||
| enforce: 'pre', | ||
| transform(code, id) { | ||
| if (id.includes('src/query-param-transformed')) { | ||
| const transformed = new MagicString(code) | ||
| const query = id.split("?query=").pop() | ||
|
|
||
| if(query === "first") { | ||
| transformed.remove( | ||
| code.indexOf("/* QUERY_PARAM FIRST START */"), | ||
| code.indexOf("/* QUERY_PARAM FIRST END */") + "/* QUERY_PARAM FIRST END */".length, | ||
| ) | ||
| } else if(query === "second") { | ||
| transformed.remove( | ||
| code.indexOf("/* QUERY_PARAM SECOND START */"), | ||
| code.indexOf("/* QUERY_PARAM SECOND END */") + "/* QUERY_PARAM SECOND END */".length, | ||
| ) | ||
| } else { | ||
| transformed.remove( | ||
| code.indexOf("/* QUERY_PARAM FIRST START */"), | ||
| code.length, | ||
| ) | ||
| } | ||
|
|
||
| const map = remapping( | ||
| [transformed.generateMap({ hires: true }), this.getCombinedSourcemap() as any], | ||
| () => null, | ||
| ) as any | ||
|
|
||
| return { code: transformed.toString(), map } | ||
| } | ||
| }, | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
test/coverage-test/fixtures/src/query-param-transformed.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| export function initial() { | ||
| return "Always present" | ||
| } | ||
|
|
||
| /* QUERY_PARAM FIRST START */ | ||
| export function first() { | ||
| return "Removed when ?query=first" | ||
| } | ||
| /* QUERY_PARAM FIRST END */ | ||
|
|
||
| /* QUERY_PARAM SECOND START */ | ||
| export function second() { | ||
| return "Removed when ?query=second" | ||
| } | ||
| /* QUERY_PARAM SECOND END */ | ||
|
|
||
| export function uncovered() { | ||
| return "Always present" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { expect, test } from 'vitest' | ||
|
|
||
| test('run initial', async () => { | ||
| const initial = await import('../src/query-param-transformed') | ||
|
|
||
| // Check that custom plugin works | ||
| expect(initial.initial()).toBe("Always present") | ||
| expect(initial.first).toBeUndefined() | ||
| expect(initial.second).toBeUndefined() | ||
| }) | ||
|
|
||
| test('run first', async () => { | ||
| const initial = await import('../src/query-param-transformed?query=first' as '../src/query-param-transformed') | ||
|
|
||
| // Check that custom plugin works | ||
| expect(initial.initial()).toBe("Always present") | ||
| expect(initial.first).toBeUndefined() | ||
| expect(initial.second()).toBe("Removed when ?query=second") | ||
| }) | ||
|
|
||
| test('run second', async () => { | ||
| const initial = await import('../src/query-param-transformed?query=second' as '../src/query-param-transformed') | ||
|
|
||
| // Check that custom plugin works | ||
| expect(initial.initial()).toBe("Always present") | ||
| expect(initial.first()).toBe("Removed when ?query=first") | ||
| expect(initial.second).toBeUndefined() | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { expect } from 'vitest' | ||
| import { readCoverageMap, runVitest, test } from '../utils' | ||
|
|
||
| test('query param based transforms are resolved properly', async () => { | ||
| await runVitest({ | ||
| config: 'fixtures/configs/vitest.config.query-param-transform.ts', | ||
| include: ['fixtures/test/query-param.test.ts'], | ||
| coverage: { reporter: 'json' }, | ||
| }) | ||
|
|
||
| const coverageMap = await readCoverageMap() | ||
|
|
||
| // Query params should not be present in final report | ||
| expect(coverageMap.files()).toMatchInlineSnapshot(` | ||
| [ | ||
| "<process-cwd>/fixtures/src/query-param-transformed.ts", | ||
| ] | ||
| `) | ||
|
|
||
| const coverage = coverageMap.fileCoverageFor(coverageMap.files()[0]) | ||
|
|
||
| // Query params change which functions end up in transform result, | ||
| // verify that all functions are present | ||
| const functionCoverage = Object.keys(coverage.fnMap) | ||
| .map(index => ({ name: coverage.fnMap[index].name, hits: coverage.f[index] })) | ||
| .sort((a, b) => a.name.localeCompare(b.name)) | ||
|
|
||
| expect(functionCoverage).toMatchInlineSnapshot(` | ||
| [ | ||
| { | ||
| "hits": 1, | ||
| "name": "first", | ||
| }, | ||
| { | ||
| "hits": 3, | ||
| "name": "initial", | ||
| }, | ||
| { | ||
| "hits": 1, | ||
| "name": "second", | ||
| }, | ||
| { | ||
| "hits": 0, | ||
| "name": "uncovered", | ||
| }, | ||
| ] | ||
| `) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
module.file || filenamedid not contain query params.module.iddoes. We need to have the query present invm.RunningCodeOptions.filenameso that V8 reports those files properly. Otherwise we would see same file URL multiple times, and could not identify which coverage belongs to which file transform.Before:
After:
Note that the offset of
functionsthere maps to different positions, based on query params.