vitest-dev / vitest-dev/vitest
Incorrect error lines reported when using vi.mock in browser mode
Nobody has claimed this yet.
- 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:
Comment out/remove the vi.mock() segment, to fix and observe the correct error report:
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
- 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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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