Missing function coverage when transform prepends code right before the function #7130
Description
Describe the bug
EDIT: I made a simpler reproduction with user land transform #7130 (comment).
related:
- fix(ssrTransform): preserve line offset when transforming imports vitejs/vite#19004
- fix(ssr): hoist export to handle cyclic import better vitejs/vite#18983
After vitejs/vite#19004, a following code is transformed in this way (source map vis)
input
export function f(a: number, b: number) {
return a + b;
}
import "node:path"
output
const __vite_ssr_import_0__ = await __vite_ssr_import__("node:path");function f(a, b) {
return a + b;
}
Object.defineProperty(__vite_ssr_exports__, "f", { enumerable: true, configurable: true, get(){ return f }});;
This seems to cause function f
to be considered not covered even when f
is executed. I confirmed Vite commit right before vitejs/vite#19004 didn't have this issue.
Independently from vitejs/vite#19004, I'm trying to hoist Object.defineProperty
in vitejs/vite#18983 and that seems to cause a similar missing coverage.
Transform works like this (source map vis)
input
export function f(a: number, b: number) {
return a + b;
}
output
Object.defineProperty(__vite_ssr_exports__, "f", { enumerable: true, configurable: true, get(){ return f }});function f(a, b) {
return a + b;
}
People usually don't write import
after the export
like the first case and we didn't have a test case for that, so I didn't notice it when reviewing vitejs/vite#19004. While testing my PR #7096, this just got caught because it broke the coverage test, which has this pattern (export function
at the first line):
Reproduction
https://github.com/hi-ogawa/reproductions/tree/main/vitest-function-coverage-vite-19004
https://github.com/hi-ogawa/reproductions/tree/main/vitest-function-coverage-prepend-transform
System Info
System:
OS: Linux 6.12 Arch Linux
CPU: (16) x64 12th Gen Intel(R) Core(TM) i7-12650H
Memory: 10.09 GB / 31.05 GB
Container: Yes
Shell: 5.2.37 - /usr/bin/bash
Binaries:
Node: 20.18.1 - ~/.volta/tools/image/node/20.18.1/bin/node
npm: 10.8.2 - ~/.volta/tools/image/node/20.18.1/bin/npm
pnpm: 9.15.1 - ~/.volta/bin/pnpm
bun: 1.1.29 - ~/.volta/bin/bun
Browsers:
Chromium: 131.0.6778.85
npmPackages:
@vitest/coverage-v8: 3.0.0-beta.3 => 3.0.0-beta.3
vitest: 3.0.0-beta.3 => 3.0.0-beta.3
Used Package Manager
pnpm
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.