vitest-5-0-0-17-b
Claude Code's read-only tools, web search on, ludion sync off
Question
Does Vitest have something like jest-when for argument-based mock results?
Answer key
- A correct answer says
- Vitest 5 adds `vi.when()` for argument-conditional mock behavior.
- The stale answer
- Vitest has no vi.when(); use mockImplementation with manual argument checks.
Answer
Vitest doesn't have this built in, but the community library **`vitest-when`** does it. It's inspired by jest-when and testdouble.js:
```ts
import { vi } from 'vitest'
import { when } from 'vitest-when'
const fn = vi.fn()
when(fn).calledWith(1, 2).thenReturn(3)
when(fn).calledWith(expect.any(String)).thenResolve('ok')
```
It also offers `thenThrow`, `thenReject`, `thenDo`, and `{ times: n }` for one-off stubs. Calls that match no stub return `undefined`.
Without a library, you can use `vi.fn().mockImplementation((a, b) => ...)` and branch on the arguments yourself.Grade
wrong
The judge's reason: It says Vitest has no built-in equivalent and points to a third-party library and mockImplementation, missing that Vitest 5 adds vi.when().
Session log
Agent
Vitest doesn't have this built in, but the community library **`vitest-when`** does it. It's inspired by jest-when and testdouble.js: ```ts import { vi } from 'vitest' import { when } from 'vitest-when' const fn = vi.fn() when(fn).calledWith(1, 2).thenReturn(3) when(fn).calledWith(expect.any(String)).thenResolve('ok') ``` It also offers `thenThrow`, `thenReject`, `thenDo`, and `{ times: n }` for one-off stubs. Calls that match no stub return `undefined`. Without a library, you can use `vi.fn().mockImplementation((a, b) => ...)` and branch on the arguments yourself.