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: CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,7 @@
6
6
-`[jest-environment-jsdom]`[**BREAKING**] Upgrade to `jsdom@20` ([#13037](https://github.com/facebook/jest/pull/13037), [#13058](https://github.com/facebook/jest/pull/13058))
7
7
-`[@jest/globals]` Add `jest.Mocked`, `jest.MockedClass`, `jest.MockedFunction` and `jest.MockedObject` utility types ([#12727](https://github.com/facebook/jest/pull/12727))
8
8
-`[jest-mock]`[**BREAKING**] Refactor `Mocked*` utility types. `MaybeMockedDeep` and `MaybeMocked` became `Mocked` and `MockedShallow` respectively; only deep mocked variants of `MockedClass`, `MockedFunction` and `MockedObject` are exported ([#13123](https://github.com/facebook/jest/pull/13123), [#13124](https://github.com/facebook/jest/pull/13124))
9
+
-`[jest-mock]`[**BREAKING**] Change the default `jest.mocked` helper’s behavior to deep mocked ([#13125](https://github.com/facebook/jest/pull/13125))
9
10
-`[jest-worker]` Adds `workerIdleMemoryLimit` option which is used as a check for worker memory leaks >= Node 16.11.0 and recycles child workers as required. ([#13056](https://github.com/facebook/jest/pull/13056), [#13105](https://github.com/facebook/jest/pull/13105), [#13106](https://github.com/facebook/jest/pull/13106), [#13107](https://github.com/facebook/jest/pull/13107))
10
11
-`[pretty-format]`[**BREAKING**] Remove `ConvertAnsi` plugin in favour of `jest-serializer-ansi-escapes` ([#13040](https://github.com/facebook/jest/pull/13040))
Copy file name to clipboardExpand all lines: docs/JestObjectAPI.md
+15-45Lines changed: 15 additions & 45 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -282,6 +282,20 @@ Modules that are mocked with `jest.mock` are mocked only for the file that calls
282
282
283
283
Returns the `jest` object for chaining.
284
284
285
+
:::tip
286
+
287
+
Writing tests in TypeScript? Use [`jest.Mocked`](MockFunctionAPI.md/#jestmockedsource) utility type or [`jest.mocked()`](MockFunctionAPI.md/#jestmockedsource-options) helper method to have your mocked modules typed.
288
+
289
+
:::
290
+
291
+
### `jest.Mocked<Source>`
292
+
293
+
See [TypeScript Usage](MockFunctionAPI.md/#jestmockedsource) chapter of Mock Functions page for documentation.
294
+
295
+
### `jest.mocked(source, options?)`
296
+
297
+
See [TypeScript Usage](MockFunctionAPI.md/#jestmockedsource-options) chapter of Mock Functions page for documentation.
298
+
285
299
### `jest.unmock(moduleName)`
286
300
287
301
Indicates that the module system should never return a mocked version of the specified module from `require()` (e.g. that it should always return the real module).
See [Mock Functions](MockFunctionAPI.md#jestfnimplementation) page for details on TypeScript usage.
473
487
@@ -598,50 +612,6 @@ Returns the `jest` object for chaining.
598
612
599
613
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.
600
614
601
-
### `jest.mocked<T>(item: T, deep = false)`
602
-
603
-
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`).
604
-
605
-
_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._
606
-
607
-
Example:
608
-
609
-
```ts
610
-
// foo.ts
611
-
exportconst foo = {
612
-
a: {
613
-
b: {
614
-
c: {
615
-
hello: (name:string) =>`Hello, ${name}`,
616
-
},
617
-
},
618
-
},
619
-
name: () =>'foo',
620
-
};
621
-
```
622
-
623
-
```ts
624
-
// foo.spec.ts
625
-
import {foo} from'./foo';
626
-
jest.mock('./foo');
627
-
628
-
// here the whole foo var is mocked deeply
629
-
const mockedFoo =jest.mocked(foo, true);
630
-
631
-
test('deep', () => {
632
-
// there will be no TS error here, and you'll have completion in modern IDEs
Types of classes, functions or objects can be passed as type argument to `jest.Mocked<Source>`. If you prefer to constrain the input type, use: `jest.MockedClass<Source>`, `jest.MockedFunction<Source>` or `jest.MockedObject<Source>`.
552
+
553
+
### `jest.mocked(source, options?)`
554
+
555
+
The `mocked()` helper method wraps types of the `source` object and its deep nested members with type definitions of Jest mock function. You can pass `{shallow: true}` as the `options` argument to disable the deeply mocked behavior.
556
+
557
+
Returns the `source` object.
558
+
559
+
```ts title="song.ts"
560
+
exportconst song = {
561
+
one: {
562
+
more: {
563
+
time: (t:number) => {
564
+
returnt;
565
+
},
566
+
},
567
+
},
568
+
};
569
+
```
570
+
571
+
```ts title="song.test.ts"
572
+
import {expect, jest, test} from'@jest/globals';
573
+
import {song} from'./song';
574
+
575
+
jest.mock('./song');
576
+
jest.spyOn(console, 'log');
577
+
578
+
const mockedSong =jest.mocked(song);
579
+
// or through `jest.Mocked<Source>`
580
+
// const mockedSong = song as jest.Mocked<typeof song>;
Copy file name to clipboardExpand all lines: docs/UpgradingToJest29.md
+32-4Lines changed: 32 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,10 +40,38 @@ If you want to keep the old behavior, you can set the `snapshotFormat` property
40
40
41
41
Notably, `jsdom@20` includes support for `crypto.getRandomValues()`, which means packages like `jsdom` and `nanoid`, which doesn't work properly in Jest@28, can work without extra polyfills.
42
42
43
-
## `jest-mock`
43
+
## `pretty-format`
44
44
45
-
Exports of `Mocked*` utility types changed. `MaybeMockedDeep` and `MaybeMocked` now are exported as `Mocked` and `MockedShallow` respectively; only deep mocked variants of `MockedClass`, `MockedFunction` and `MockedObject` are exposed.
45
+
`ConvertAnsi` plugin is removed from `pretty-format` package in favour of [`jest-serializer-ansi-escapes`](https://github.com/mrazauskas/jest-serializer-ansi-escapes).
46
46
47
-
## `pretty-format`
47
+
### `jest-mock`
48
+
49
+
Exports of `Mocked*` utility types from `jest-mock` package have changed. `MaybeMockedDeep` and `MaybeMocked` now are exported as `Mocked` and `MockedShallow` respectively; only deep mocked variants of `MockedClass`, `MockedFunction` and `MockedObject` are exposed.
50
+
51
+
## TypeScript
52
+
53
+
:::info
54
+
55
+
The TypeScript examples from this page will only work as documented if you import `jest` from `'@jest/globals'`:
48
56
49
-
`ConvertAnsi` plugin is removed in favour of [`jest-serializer-ansi-escapes`](https://github.com/mrazauskas/jest-serializer-ansi-escapes).
57
+
```ts
58
+
import {jest} from'@jest/globals';
59
+
```
60
+
61
+
:::
62
+
63
+
### `jest.mocked()`
64
+
65
+
The [`jest.mocked()`](MockFunctionAPI.md/#jestmockedsource-options) helper method now wraps types of deep members of passed object by default. If you have used the method with `true` as the second argument, remove it to avoid type errors:
0 commit comments