You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Configures a `vi.fn()` or `vi.spyOn()` mock function to act as a vitest-when stub. Adds an implementation to the function that initially no-ops, and returns an API to configure behaviors for given arguments using [`.calledWith(...)`][called-with]
190
190
191
191
```ts
192
192
import { vi } from'vitest'
193
193
import { when } from'vitest-when'
194
194
195
-
constspy=vi.fn()
195
+
constmock=vi.fn()
196
196
197
-
when(spy)
197
+
when(mock)
198
198
199
-
expect(spy()).toBe(undefined)
199
+
expect(mock()).toBe(undefined)
200
200
```
201
201
202
202
#### Options
@@ -209,36 +209,32 @@ import type { WhenOptions } from 'vitest-when'
Create a stub that matches a given set of arguments which you can configure with different behaviors using methods like [`.thenReturn(...)`][then-return].
When a call to a mock uses arguments that match those given to `calledWith`, a configured behavior will be triggered. All arguments must match, but you can use Vitest's [asymmetric matchers][] to loosen the stubbing:
By default, if arguments do not match, a vitest-when stub will no-op and return `undefined`. You can customize this fallback by configuring your own unconditional behavior on the mock using Vitest's built-in [mock API][].
You may pass several values to `thenReturn` to return different values in succession. If you do not specify `times`, the last value will be latched. Otherwise, each value will be returned the specified number of times.
You may pass several values to `thenResolve` to resolve different values in succession. If you do not specify `times`, the last value will be latched. Otherwise, each value will be resolved the specified number of times.
You may pass several values to `thenThrow` to throw different errors in succession. If you do not specify `times`, the last value will be latched. Otherwise, each error will be thrown the specified number of times.
382
366
383
367
```ts
384
-
const spy =vi.fn()
385
-
386
-
when(spy)
368
+
const mock =when(vi.fn())
387
369
.calledWith('hello')
388
370
.thenThrow(newError('oh no'), newError('this is bad'))
389
371
390
-
expect(() =>spy('hello')).toThrow('oh no')
391
-
expect(() =>spy('hello')).toThrow('this is bad')
392
-
expect(() =>spy('hello')).toThrow('this is bad')
372
+
expect(() =>mock('hello')).toThrow('oh no')
373
+
expect(() =>mock('hello')).toThrow('this is bad')
374
+
expect(() =>mock('hello')).toThrow('this is bad')
393
375
```
394
376
395
-
### `.thenReject(error: unknown)`
377
+
### `.thenReject(error: unknown) -> Mock<TFunc>`
396
378
397
379
When the stubbing is satisfied, reject a `Promise` with `error`.
You may pass several values to `thenReject` to throw different errors in succession. If you do not specify `times`, the last value will be latched. Otherwise, each rejection will be triggered the specified number of times.
421
401
422
402
```ts
423
-
const spy =vi.fn()
424
-
425
-
when(spy)
403
+
const mock =when(vi.fn())
426
404
.calledWith('hello')
427
405
.thenReject(newError('oh no'), newError('this is bad'))
When the stubbing is satisfied, run `callback` to trigger a side-effect and return its result (if any). `thenDo` is a relatively powerful tool for stubbing complex behaviors, so if you find yourself using `thenDo` often, consider refactoring your code to use more simple interactions! Your future self will thank you.
437
415
438
416
```ts
439
-
const spy =vi.fn()
440
417
let called =false
441
418
442
-
when(spy)
419
+
const mock =when(vi.fn())
443
420
.calledWith('hello')
444
421
.thenDo(() => {
445
422
called=true
446
423
return'world'
447
424
})
448
425
449
-
expect(spy('hello')).toEqual('world')
426
+
expect(mock('hello')).toEqual('world')
450
427
expect(called).toEqual(true)
451
428
```
452
429
@@ -455,33 +432,29 @@ To only run the callback once, use the `times` option.
455
432
```ts
456
433
import { times, when } from'vitest-when'
457
434
458
-
const spy =vi.fn()
459
-
460
-
when(spy, { times: 1 })
435
+
const mock =when(vi.fn(), { times: 1 })
461
436
.calledWith('hello')
462
437
.thenDo(() =>'world')
463
438
464
-
expect(spy('hello')).toEqual('world')
465
-
expect(spy('hello')).toEqual(undefined)
439
+
expect(mock('hello')).toEqual('world')
440
+
expect(mock('hello')).toEqual(undefined)
466
441
```
467
442
468
443
You may pass several callbacks to `thenDo` to trigger different side-effects in succession. If you do not specify `times`, the last callback will be latched. Otherwise, each callback will be triggered the specified number of times.
0 commit comments