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
Copy file name to clipboardExpand all lines: README.md
+92-3Lines changed: 92 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -37,6 +37,7 @@ Wrap your `vi.fn()` mock - or a function imported from a `vi.mock`'d module - in
37
37
-[`.thenThrow()`][then-throw] - Throw an error
38
38
-[`.thenReject()`][then-reject] - Reject a `Promise`
39
39
-[`.thenDo()`][then-do] - Trigger a function
40
+
-[`.thenCallback()`][then-callback] - Invoke a callback argument
40
41
41
42
If the stub is called with arguments that match `calledWith`, the configured behavior will occur. If the arguments do not match, the stub will no-op and return `undefined`.
42
43
@@ -76,6 +77,7 @@ You should call `vi.resetAllMocks()` in your suite's `afterEach` hook to remove
When the stubbing is satisfied, invoke a callback function passed as an argument with the specified `args`. Use with [`expect.callback()`][expect-callback] to match callback arguments.
463
+
464
+
```ts
465
+
const mock =when(vi.fn())
466
+
.calledWith('data', expect.callback())
467
+
.thenCallback('result')
468
+
469
+
mock('data', (value) => {
470
+
console.log(value) // 'result'
471
+
})
472
+
```
473
+
474
+
If you don't specify `expect.callback()` in `calledWith`, an implied callback matcher is added as the last argument:
This is the same as `expect.callback()`, but exported as `expectCallback` for use when the your tests use the context's `expect` function (e.g. `it('test', ({ expect }) => { ... }))`).
0 commit comments