Skip to content

Exception stack trace line numbers are wrong in browser mode #5996

@kwojcik

Description

@kwojcik

Describe the bug

While running in browser mode, exception stack trace line numbers do not match source code.

See this trace showing line 15. It should be 18.

ReferenceError: expected is not defined
 ❯ Link ../../../../../components/Link.tsx:15:15
 ❯ renderWithHooks ../../../../../node_modules/.vite/deps/@testing-library_react.js:11559:26
 ....

When running in jsdom instead of browser mode the stack is correct and even includes a code snippet

ReferenceError: expected is not defined
 ❯ Link components/Link.tsx:18:15
     16|     setStatus(STATUS.NORMAL);
     17|   };
     18|   console.log(expected.error.here); // <<---------- This is line 18
       |               ^
     19|
     20|   return (
 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom.development.js:15486:18

For this source code from the vitest examples

import React, { useState } from "react";

const STATUS = {
  HOVERED: "hovered",
  NORMAL: "normal",
};

function Link({ page, children }: React.PropsWithChildren<{ page: string }>) {
  const [status, setStatus] = useState(STATUS.NORMAL);

  const onMouseEnter = () => {
    setStatus(STATUS.HOVERED);
  };

  const onMouseLeave = () => {
    setStatus(STATUS.NORMAL);
  };
  console.log(expected.error.here); // <<---------- This is line 18

  return (
    <a
      className={status}
      href={page || "#"}
      aria-label={`Link is ${status}`}
      onMouseEnter={onMouseEnter}
      onMouseLeave={onMouseLeave}
    >
      {children}
    </a>
  );
}

export default Link;

Reproduction

https://github.com/kwojcik/vitestsourcemapbug

npm install
npm run test

System Info

System:
    OS: macOS 13.6.7
    CPU: (12) arm64 Apple M2 Max
    Memory: 14.71 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.21 - /opt/homebrew/bin/yarn
    npm: 9.8.1 - ~/.asdf/plugins/nodejs/shims/npm
    pnpm: 6.32.8 - /opt/homebrew/bin/pnpm
    Watchman: 2024.04.01.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 126.0.6478.127
    Safari: 16.6
  npmPackages:
    @vitest/browser: ^1.6.0 => 1.6.0
    @vitest/ui: latest => 1.6.0
    vite: latest => 5.3.2
    vitest: latest => 1.6.0


### Used Package Manager

npm

### Validations

- [X] Follow our [Code of Conduct](https://github.com/vitest-dev/vitest/blob/main/CODE_OF_CONDUCT.md)
- [X] Read the [Contributing Guidelines](https://github.com/vitest-dev/vitest/blob/main/CONTRIBUTING.md).
- [X] Read the [docs](https://vitest.dev/guide/).
- [X] Check that there isn't [already an issue](https://github.com/vitest-dev/vitest/issues) that reports the same bug to avoid creating a duplicate.
- [X] Check that this is a concrete bug. For Q&A open a [GitHub Discussion](https://github.com/vitest-dev/vitest/discussions) or join our [Discord Chat Server](https://chat.vitest.dev).
- [X] The provided reproduction is a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of the bug.

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: browserIssues and PRs related to the browser runnerp3-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