vitest-dev / vitest-dev/vitest

Incorrect error lines reported when using vi.mock in browser mode

Open
#7,561 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

feat: browser p3-minor-bug
Dominant language
TypeScript
Stars
17.1k
Forks
2k
Avg merge
1d 21h
Merged PRs (30d)
92

Description

Describe the bug

Using vi.mock in browser mode causes the reported error lines to be incorrect when the test fails

Reproduction
Repo with reproducible behaviour

https://github.com/omricundangan/vitest-demo-errolines
Repo readme has exact steps on how to reproduce the error

Steps

In a clean install of vite (npm create vite@latest) configured for Javascript + React and vitest browser with playwright (npx vitest init browser), create two files: one module, and one testing + mocking the module:

performOp.js:

export const performOp = (a, b) => {
  return a + b
}

performOp.test.js:

import { expect, it, vi } from "vitest"
import { performOp } from "./performOp"

vi.mock('./performOp', () => ({
  performOp: vi.fn().mockReturnValue(10),
}))

it('tests performOp module', () => {
  expect(performOp(1, 2)).toBe(5)
})

Run vitest --workspace=vitest.workspace.js to produce the following output and observe the incorrect error lines reported:

Image

Comment out/remove the vi.mock() segment, to fix and observe the correct error report:

Image

System Info
System:
    OS: macOS 15.3
    CPU: (12) arm64 Apple M3 Pro
    Memory: 173.53 MB / 36.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.9.0 - ~/.nvm/versions/node/v22.9.0/bin/node
    npm: 10.8.3 - ~/.nvm/versions/node/v22.9.0/bin/npm
  Browsers:
    Chrome: 133.0.6943.127
    Safari: 18.3
  npmPackages:
    @vitejs/plugin-react: ^4.3.4 => 4.3.4 
    @vitest/browser: ^3.0.7 => 3.0.7 
    playwright: ^1.50.1 => 1.50.1 
    vite: ^6.2.0 => 6.2.0 
    vitest: ^3.0.7 => 3.0.7 
    vitest-browser-react: ^0.1.1 => 0.1.1
Used Package Manager

npm

Validations

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproduction repository and its README, then run the two-file performOp.js and performOp.test.js example through the Vitest browser workspace with Playwright. Compare the reported locations with and without the vi.mock block; done means the failing assertion reports the correct error lines when mocking is enabled.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, playwright
Domain
testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.