Skip to content

Missing function coverage when transform prepends code right before the function #7130

Open
@hi-ogawa

Description

Describe the bug

EDIT: I made a simpler reproduction with user land transform #7130 (comment).

related:

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):

export function sum(a: number, b: number) {

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

Metadata

Assignees

No one assigned

    Labels

    feat: coverageIssues and PRs related to the coverage featurep3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions