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/JestObjectAPI.md
+44Lines changed: 44 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -578,6 +578,50 @@ Returns the `jest` object for chaining.
578
578
579
579
Restores all mocks back to their original value. Equivalent to calling [`.mockRestore()`](MockFunctionAPI.md#mockfnmockrestore) on every mocked function. Beware that `jest.restoreAllMocks()` only works when the mock was created with `jest.spyOn`; other mocks will require you to manually restore them.
580
580
581
+
### `jest.mocked<T>(item: T, deep = false)`
582
+
583
+
The `mocked` test helper provides typings on your mocked modules and even their deep methods, based on the typing of its source. It makes use of the latest TypeScript feature, so you even have argument types completion in the IDE (as opposed to `jest.MockInstance`).
584
+
585
+
_Note: while it needs to be a function so that input type is changed, the helper itself does nothing else than returning the given input value._
586
+
587
+
Example:
588
+
589
+
```ts
590
+
// foo.ts
591
+
exportconst foo = {
592
+
a: {
593
+
b: {
594
+
c: {
595
+
hello: (name:string) =>`Hello, ${name}`,
596
+
},
597
+
},
598
+
},
599
+
name: () =>'foo',
600
+
};
601
+
```
602
+
603
+
```ts
604
+
// foo.spec.ts
605
+
import {foo} from'./foo';
606
+
jest.mock('./foo');
607
+
608
+
// here the whole foo var is mocked deeply
609
+
const mockedFoo =jest.mocked(foo, true);
610
+
611
+
test('deep', () => {
612
+
// there will be no TS error here, and you'll have completion in modern IDEs
0 commit comments