vitest-5-0-0-17-b
Claude Code's read-only tools, web search on, ludion sync on
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
Yes. Vitest 5.0 added `vi.when`, and this project has vitest 5.0.3 installed, so you can use it. I confirmed it in the installed type definitions (`node_modules/vitest/dist/index.d.ts`). It takes a spy and lets you set results per argument, much like jest-when:
```ts
const spy = vi.fn()
vi.when(spy).calledWith(1).thenReturn(0)
vi.when(spy).calledWith('user').thenResolve({ id: 1 })
```
You can also pass `{ onUnmatched: 'throw' }` so a call with arguments you didn't set up throws an error. On Vitest versions before 5, use `mockImplementation` and branch on the arguments yourself.Grade
correct
The judge's reason: It states that Vitest 5 added vi.when for argument-specific mock results and gives mockImplementation only as the approach for pre-5 versions, not as current behavior.
Session log
Tool call: Grep
{ "pattern": "\\bwhen\\b\\s*[:(<]", "path": "C:\\Users\\haya0\\AppData\\Local\\Temp\\ludion-bench-ncSKNF\\work\\node_modules\\vitest\\dist", "glob": "*.d.ts", "output_mode": "content", "head_limit": 10 }Tool output
node_modules\vitest\dist\index.d.ts:189:* const w = vi.when(spy).calledWith(1).thenReturn(0) node_modules\vitest\dist\index.d.ts:212:* vi.when(spy) node_modules\vitest\dist\index.d.ts:292:* using w = vi.when(spy) node_modules\vitest\dist\index.d.ts:306: * vi.when(spy) node_modules\vitest\dist\index.d.ts:312: * vi.when(spy) node_modules\vitest\dist\index.d.ts:335: * vi.when(spy, { onUnmatched: 'throw' }) node_modules\vitest\dist\index.d.ts:366:* vi.when(spy).calledWith(1).thenReturn(0) node_modules\vitest\dist\index.d.ts:374:* vi.when(spy).calledWith('user').thenResolve({ id: 1 }) node_modules\vitest\dist\index.d.ts:383:* using w = vi.when(spy) node_modules\vitest\dist\index.d.ts:395:* vi.when(spy, { onUnmatched: 'throw' }) [Showing results with pagination = limit: 10]Agent
Yes. Vitest 5.0 added `vi.when`, and this project has vitest 5.0.3 installed, so you can use it. I confirmed it in the installed type definitions (`node_modules/vitest/dist/index.d.ts`). It takes a spy and lets you set results per argument, much like jest-when: ```ts const spy = vi.fn() vi.when(spy).calledWith(1).thenReturn(0) vi.when(spy).calledWith('user').thenResolve({ id: 1 }) ``` You can also pass `{ onUnmatched: 'throw' }` so a call with arguments you didn't set up throws an error. On Vitest versions before 5, use `mockImplementation` and branch on the arguments yourself.