Claude Opus 5.5

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.

The change's lesson

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.

Answering cost $0.06.

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().

The judge's log

Session log

What the agent wrote, the tools it called, and what they returned, from docs/bench/2026-10-11-done3/runs/B-project/vitest-5-0-0-17-b.jsonl. Thinking and the session's settings are left out. The raw log

  1. 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.