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: docs/api/mock.md
+48-36Lines changed: 48 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
-
# Mock Functions
1
+
# Mocks
2
2
3
-
You can create a mock function to track its execution with `vi.fn` method. If you want to track a method on an already created object, you can use `vi.spyOn` method:
3
+
You can create a mock function or a class to track its execution with the `vi.fn` method. If you want to track a property on an already created object, you can use the`vi.spyOn` method:
4
4
5
5
```js
6
6
import { vi } from'vitest'
@@ -18,7 +18,7 @@ market.getApples()
18
18
getApplesSpy.mock.calls.length===1
19
19
```
20
20
21
-
You should use mock assertions (e.g., [`toHaveBeenCalled`](/api/expect#tohavebeencalled)) on [`expect`](/api/expect) to assert mock result. This API reference describes available properties and methods to manipulate mock behavior.
21
+
You should use mock assertions (e.g., [`toHaveBeenCalled`](/api/expect#tohavebeencalled)) on [`expect`](/api/expect) to assert mock results. This API reference describes available properties and methods to manipulate mock behavior.
22
22
23
23
::: tip
24
24
The custom function implementation in the types below is marked with a generic `<T>`.
@@ -30,7 +30,7 @@ The custom function implementation in the types below is marked with a generic `
function mockImplementationOnce(fn: T): MockInstance<T>
105
+
function mockImplementationOnce(fn: T): Mock<T>
106
106
```
107
107
108
108
Acceptsafunction to be used as the mock implementation. TypeScript expects the arguments and return type to match those of the original function. This method can be chained to produce different results for multiple function calls.
@@ -270,7 +268,7 @@ To automatically call this method before each test, enable the [`restoreMocks`](
270
268
## mockResolvedValue
271
269
272
270
```ts
273
-
function mockResolvedValue(value: Awaited<ReturnType<T>>): MockInstance<T>
271
+
function mockResolvedValue(value: Awaited<ReturnType<T>>): Mock<T>
274
272
```
275
273
276
274
Acceptsavaluethatwillberesolvedwhentheasyncfunction is called. TypeScript will only accept values that match the return type of the original function.
@@ -284,7 +282,7 @@ await asyncMock() // 42
284
282
## mockResolvedValueOnce
285
283
286
284
```ts
287
-
function mockResolvedValueOnce(value: Awaited<ReturnType<T>>): MockInstance<T>
285
+
function mockResolvedValueOnce(value: Awaited<ReturnType<T>>): Mock<T>
288
286
```
289
287
290
288
Acceptsavaluethatwillberesolvedduringthenextfunction call. TypeScript will only accept values that match the return type of the original function. If chained, each consecutive call will resolve the specified value.
function mockReturnValue(value: ReturnType<T>): MockInstance<T>
320
+
function mockReturnValue(value: ReturnType<T>): Mock<T>
323
321
```
324
322
325
323
Acceptsavaluethatwillbereturnedwheneverthemockfunction is called. TypeScript will only accept values that match the return type of the original function.
@@ -335,7 +333,7 @@ mock() // 43
335
333
## mockReturnValueOnce
336
334
337
335
```ts
338
-
function mockReturnValueOnce(value: ReturnType<T>): MockInstance<T>
336
+
function mockReturnValueOnce(value: ReturnType<T>): Mock<T>
339
337
```
340
338
341
339
Acceptsavaluethatwillbereturnedwheneverthemockfunction is called. TypeScript will only accept values that match the return type of the original function.
@@ -379,7 +377,7 @@ fn.mock.calls === [
379
377
const lastCall: Parameters<T> | undefined
380
378
```
381
379
382
-
Thiscontainstheargumentsofthelastcall. Ifmockwasn't called, it will return `undefined`.
380
+
Thiscontainstheargumentsofthelastcall. Ifthemockwasn't called, it will return `undefined`.
* If function returned a Promise, then this will be a resolved value.
389
+
* If the function returned a Promise, then this will be a resolved value.
392
390
*/
393
391
value: T
394
392
}
@@ -418,6 +416,7 @@ This is an array containing all values that were `returned` from the function. O
418
416
419
417
-`'return'`-function returned without throwing.
420
418
- `'throw'` - function threw a value.
419
+
- `'incomplete'` - the function did not finish running yet.
421
420
422
421
The`value`propertycontainsthereturnedvalueorthrownerror. Ifthefunction returned a `Promise`, then `result` will always be `'return'` even if the promise was rejected.
Thispropertyisanarraycontainingallinstancesthatwerecreatedwhenthemockwascalledwiththe`new`keyword. Notethatthisisanactualcontext (`this`) ofthefunction, not a return value.
548
+
Thispropertyisanarraycontainingallinstancesthatwerecreatedwhenthemockwascalledwiththe`new`keyword. Notethatthisistheactualcontext (`this`) ofthefunction, not a return value.
0 commit comments