vitest-dev / vitest

Next generation testing framework powered by Vite.
https://vitest.dev
MIT License
12.61k stars 1.13k forks source link

stack trace incorrect when react component throws with istanbul coverage #6116

Open kwojcik opened 1 month ago

kwojcik commented 1 month ago

Describe the bug

When a test imports a react component which throw an error, the stack trace will be incorrect with istanbul coverage. Traces are wrong (but different) in both browser and jsdom modes. The trace is correct without --coverage.

Error: oops

Failure screenshot:
  - test/__screenshots__/...

 ❯ Bar components/Bar.tsx:2:9
      1| function Foo() {
      2|   return <>hi</>;
       |         ^
      3| }
      4| export default function Bar() {

When the file looks like

function Foo() {
  return <>hi</>;
}
export default function Bar() {
  throw new Error("oops");

  return <Foo />;
}

Reproduction

https://github.com/kwojcik/vitestsourcemapbug/pull/new/reactErrorStackTrace

git checkout reactErrorStackTrace
npm install
npm run test

System Info

System:
    OS: macOS 13.6.7
    CPU: (12) arm64 Apple M2 Max
    Memory: 20.87 GB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.18.2 - ~/.asdf/installs/nodejs/18.18.2/bin/node
    Yarn: 1.22.22 - ~/.asdf/installs/nodejs/18.18.2/bin/yarn
    npm: 9.8.1 - ~/.asdf/plugins/nodejs/shims/npm
    pnpm: 9.5.0 - ~/.asdf/installs/nodejs/18.18.2/bin/pnpm
    Watchman: 2024.04.01.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 126.0.6478.127
    Firefox Nightly: 127.0
    Safari: 16.6
  npmPackages:
    @vitest/browser: 2.0.2 => 2.0.2
    @vitest/coverage-istanbul: ^2.0.2 => 2.0.2
    @vitest/ui: 2.0.2 => 2.0.2
    vite: latest => 5.3.3
    vitest: 2.0.2 => 2.0.2

Used Package Manager

npm

Validations

ekohilas commented 1 month ago

Also getting this problem in node, since at least version 1.2.2, if that helps with debugging.

AriPerkkio commented 1 month ago

Has this ever worked correctly? I just tested 2.0.2, 1.2.2, 1.0.0 and 0.33.0. The error is shown incorrectly on all those versions.