From 56b44cf9d90b30ee35f90c33f123bf2d60318abe Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Fri, 12 Aug 2022 15:12:40 +0300 Subject: [PATCH 01/12] feat(jest-mock): change `jest.mocked()` defaults --- docs/JestObjectAPI.md | 44 ----------------- docs/MockFunctionAPI.md | 47 +++++++++++++++++++ packages/jest-environment/src/index.ts | 9 ++-- packages/jest-mock/src/index.ts | 15 +++--- .../jest-types/__typetests__/jest.test.ts | 28 +++++++---- 5 files changed, 81 insertions(+), 62 deletions(-) diff --git a/docs/JestObjectAPI.md b/docs/JestObjectAPI.md index c68f4d009133..072433a30176 100644 --- a/docs/JestObjectAPI.md +++ b/docs/JestObjectAPI.md @@ -598,50 +598,6 @@ Returns the `jest` object for chaining. 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. -### `jest.mocked(item: T, deep = false)` - -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`). - -_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._ - -Example: - -```ts -// foo.ts -export const foo = { - a: { - b: { - c: { - hello: (name: string) => `Hello, ${name}`, - }, - }, - }, - name: () => 'foo', -}; -``` - -```ts -// foo.spec.ts -import {foo} from './foo'; -jest.mock('./foo'); - -// here the whole foo var is mocked deeply -const mockedFoo = jest.mocked(foo, true); - -test('deep', () => { - // there will be no TS error here, and you'll have completion in modern IDEs - mockedFoo.a.b.c.hello('me'); - // same here - expect(mockedFoo.a.b.c.hello.mock.calls).toHaveLength(1); -}); - -test('direct', () => { - foo.name(); - // here only foo.name is mocked (or its methods if it's an object) - expect(jest.mocked(foo.name).mock.calls).toHaveLength(1); -}); -``` - ## Fake Timers ### `jest.useFakeTimers(fakeTimersConfig?)` diff --git a/docs/MockFunctionAPI.md b/docs/MockFunctionAPI.md index db8f2d5ea4b3..6dff0ba98b5c 100644 --- a/docs/MockFunctionAPI.md +++ b/docs/MockFunctionAPI.md @@ -520,3 +520,50 @@ test('calculate calls add', () => { expect(mockAdd).toBeCalledWith(1, 2); }); ``` + +### `jest.mocked(source: T, {shallow?: boolean})` + +The `mocked()` type helper method wraps `source` object and its deep nested members with type definitions of Jest mock function. You can pass `{shallow: true}` option to disable the deeply mocked behavior. + +Returns the input value. + +Example: + +```ts title="song.ts" +export const song = { + one: { + more: { + time: (t: number) => { + return t; + }, + }, + }, +}; +``` + +```ts title="song.test.ts" +import {expect, jest, test} from '@jest/globals'; +import {song} from './song'; + +jest.mock('./song'); +jest.spyOn(console, 'log'); + +const mockedSong = jest.mocked(song); + +test('deep method is typed correctly', () => { + mockedSong.one.more.time.mockReturnValue(12); + + expect(mockedSong.one.more.time(10)).toBe(12); + expect(mockedSong.one.more.time.mock.calls).toHaveLength(1); +}); + +test('direct usage', () => { + jest.mocked(console.log).mockImplementation(() => { + return; + }); + + console.log('one more time'); + + expect(jest.mocked(console.log).mock.calls).toHaveLength(1); +}); +``` diff --git a/packages/jest-environment/src/index.ts b/packages/jest-environment/src/index.ts index 7a6e1cc8f7ef..f03a3615a119 100644 --- a/packages/jest-environment/src/index.ts +++ b/packages/jest-environment/src/index.ts @@ -201,6 +201,11 @@ export interface Jest { ``` */ requireActual: (moduleName: string) => unknown; + /** + * Wraps `source` object and its deep members with type definitions of Jest mock function. + * Pass `{shallow: true}` option to disable the deeply mocked behavior. + */ + mocked: ModuleMocker['mocked']; /** * Returns a mock module instead of the actual module, bypassing all checks * on whether the module should be required normally or not. @@ -224,10 +229,6 @@ export interface Jest { * with `jest.spyOn()`; other mocks will require you to manually restore them. */ restoreAllMocks(): Jest; - /** - * Wraps an object or a module with mock type definitions. - */ - mocked: ModuleMocker['mocked']; /** * Runs failed tests n-times until they pass or until the max number of * retries is exhausted. diff --git a/packages/jest-mock/src/index.ts b/packages/jest-mock/src/index.ts index 78f3fcc49389..305699b0a50c 100644 --- a/packages/jest-mock/src/index.ts +++ b/packages/jest-mock/src/index.ts @@ -83,7 +83,7 @@ export type Mocked = T extends ClassLike ? MockedObject : T; -type MockedShallow = T extends ClassLike +export type MockedShallow = T extends ClassLike ? MockedClass : T extends FunctionLike ? MockedFunctionShallow @@ -1216,13 +1216,16 @@ export class ModuleMocker { return value == null ? `${value}` : typeof value; } - mocked(item: T, deep?: false): MockedShallow; - mocked(item: T, deep: true): Mocked; + mocked(source: T, options?: {shallow: false}): Mocked; mocked( - item: T, - _deep = false, + source: T, + options: {shallow: true}, + ): MockedShallow; + mocked( + source: T, + _options?: {shallow: boolean}, ): Mocked | MockedShallow { - return item as Mocked | MockedShallow; + return source as Mocked | MockedShallow; } } diff --git a/packages/jest-types/__typetests__/jest.test.ts b/packages/jest-types/__typetests__/jest.test.ts index 58f6a1020ffa..8bb08012566c 100644 --- a/packages/jest-types/__typetests__/jest.test.ts +++ b/packages/jest-types/__typetests__/jest.test.ts @@ -7,7 +7,13 @@ import {expectAssignable, expectError, expectType} from 'tsd-lite'; import {jest} from '@jest/globals'; -import type {Mock, ModuleMocker, SpyInstance} from 'jest-mock'; +import type { + Mock, + Mocked, + MockedShallow, + ModuleMocker, + SpyInstance, +} from 'jest-mock'; expectType( jest @@ -210,7 +216,7 @@ expectType(jest.fn); expectType(jest.spyOn); -// deep mocked() +// mocked() class SomeClass { constructor(one: string, two?: boolean) {} @@ -248,9 +254,17 @@ const someObject = { someClassInstance: new SomeClass('value'), }; -const mockObjectA = jest.mocked(someObject, true); +expectType>(jest.mocked(someObject)); +expectType>( + jest.mocked(someObject, {shallow: false}), +); +expectType>( + jest.mocked(someObject, {shallow: true}), +); + +expectError(jest.mocked('abc')); -expectError(jest.mocked('abc', true)); +const mockObjectA = jest.mocked(someObject); expectType<[]>(mockObjectA.methodA.mock.calls[0]); expectType<[b: string]>(mockObjectA.methodB.mock.calls[0]); @@ -303,11 +317,9 @@ expectError( expectAssignable(mockObjectA); -// mocked() - -const mockObjectB = jest.mocked(someObject); +// shallow mocked() -expectError(jest.mocked('abc')); +const mockObjectB = jest.mocked(someObject, {shallow: true}); expectType<[]>(mockObjectB.methodA.mock.calls[0]); expectType<[b: string]>(mockObjectB.methodB.mock.calls[0]); From 7524205dcc4a25ced13a8096ddee7a08da973a43 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Fri, 12 Aug 2022 15:28:48 +0300 Subject: [PATCH 02/12] add upgrading guide --- docs/UpgradingToJest29.md | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/docs/UpgradingToJest29.md b/docs/UpgradingToJest29.md index 2dad8da9854f..4f7736108893 100644 --- a/docs/UpgradingToJest29.md +++ b/docs/UpgradingToJest29.md @@ -40,10 +40,38 @@ If you want to keep the old behavior, you can set the `snapshotFormat` property 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. -## `jest-mock` +## `pretty-format` -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. +`ConvertAnsi` plugin is removed from `pretty-format` in favour of [`jest-serializer-ansi-escapes`](https://github.com/mrazauskas/jest-serializer-ansi-escapes). -## `pretty-format` +### `jest-mock` + +Exports of `Mocked*` utility types from `jest-mock` module have changed. `MaybeMockedDeep` and `MaybeMocked` now are exported as `Mocked` and `MockedShallow` respectively; only deep mocked variants of `MockedClass`, `MockedFunction` and `MockedObject` are exposed. + +## TypeScript + +:::info + +The TypeScript examples from this page will only work as documented if you import `jest` from `'@jest/globals'`: -`ConvertAnsi` plugin is removed in favour of [`jest-serializer-ansi-escapes`](https://github.com/mrazauskas/jest-serializer-ansi-escapes). +```ts +import {jest} from '@jest/globals'; +``` + +::: + +### `jest.mocked()` + +The `mocked()` method now wraps deep members of passed object by default. If you used the method with `true` argument before, remove it to avoid type errors: + +```diff +- const mockedObject = jest.mocked(someObject, true); ++ const mockedObject = jest.mocked(someObject); +``` + +To have the old shallow mocked behavior, pass `{shallow: true}` option: + +```diff +- const mockedObject = jest.mocked(someObject); ++ const mockedObject = jest.mocked(someObject, {shallow: true}); +``` From d5909de614460fff16d0a9a4e2401677a0b9292f Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Fri, 12 Aug 2022 15:49:21 +0300 Subject: [PATCH 03/12] add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02d6f94c4885..4cc7800d5662 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - `[jest-config]` [**BREAKING**] Make `snapshotFormat` default to `escapeString: false` and `printBasicPrototype: false` ([#13036](https://github.com/facebook/jest/pull/13036)) - `[jest-environment-jsdom]` [**BREAKING**] Upgrade to `jsdom@20` ([#13037](https://github.com/facebook/jest/pull/13037), [#13058](https://github.com/facebook/jest/pull/13058)) - `[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)) +- `[jest-mock]` [**BREAKING**] Change the default `jest.mocked` helper’s behavior to deep mocked ([#13125](https://github.com/facebook/jest/pull/13125)) - `[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)) - `[pretty-format]` [**BREAKING**] Remove `ConvertAnsi` plugin in favour of `jest-serializer-ansi-escapes` ([#13040](https://github.com/facebook/jest/pull/13040)) From d5e7f7b19e578eb5e5d14a67c2f78cd3fe87626c Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Fri, 12 Aug 2022 15:50:28 +0300 Subject: [PATCH 04/12] fix typo --- docs/UpgradingToJest29.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/UpgradingToJest29.md b/docs/UpgradingToJest29.md index 4f7736108893..bf42cdc34555 100644 --- a/docs/UpgradingToJest29.md +++ b/docs/UpgradingToJest29.md @@ -42,11 +42,11 @@ Notably, `jsdom@20` includes support for `crypto.getRandomValues()`, which means ## `pretty-format` -`ConvertAnsi` plugin is removed from `pretty-format` in favour of [`jest-serializer-ansi-escapes`](https://github.com/mrazauskas/jest-serializer-ansi-escapes). +`ConvertAnsi` plugin is removed from `pretty-format` package in favour of [`jest-serializer-ansi-escapes`](https://github.com/mrazauskas/jest-serializer-ansi-escapes). ### `jest-mock` -Exports of `Mocked*` utility types from `jest-mock` module have changed. `MaybeMockedDeep` and `MaybeMocked` now are exported as `Mocked` and `MockedShallow` respectively; only deep mocked variants of `MockedClass`, `MockedFunction` and `MockedObject` are exposed. +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. ## TypeScript From 6db36fad036a9024311128db8356d5ec1627a428 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Sat, 13 Aug 2022 08:17:55 +0300 Subject: [PATCH 05/12] tweak docs --- docs/MockFunctionAPI.md | 12 ++++++++---- docs/UpgradingToJest29.md | 2 +- packages/jest-environment/src/index.ts | 5 +++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/docs/MockFunctionAPI.md b/docs/MockFunctionAPI.md index 1c7fb0e93411..20f5b51653ce 100644 --- a/docs/MockFunctionAPI.md +++ b/docs/MockFunctionAPI.md @@ -526,13 +526,17 @@ test('calculate calls add', () => { The `jest.Mocked` utility type returns the `Source` type wrapped with type definitions of Jest mock function. ```ts -import fetch from 'node-fetch'; import {expect, jest, test} from '@jest/globals'; +import type {fetch} from './fetch'; jest.mock('node-fetch'); let mockedFetch: jest.Mocked; +afterEach(() => { + mockedFetch = resetMockedFetch(); +}); + test('makes correct call', () => { mockedFetch = getMockedFetch(); // ... @@ -548,12 +552,10 @@ Types of classes, functions or objects can be passed as type argument to `jest.M ### `jest.mocked(source: T, {shallow?: boolean})` -The `mocked()` type helper method wraps `source` object and its deep nested members with type definitions of Jest mock function. You can pass `{shallow: true}` option to disable the deeply mocked behavior. +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}` option to disable the deeply mocked behavior. Returns the input value. -Example: - ```ts title="song.ts" export const song = { one: { @@ -574,6 +576,8 @@ jest.mock('./song'); jest.spyOn(console, 'log'); const mockedSong = jest.mocked(song); +// or through `jest.Mocked` +// const mockedSong = song as jest.Mocked; test('deep method is typed correctly', () => { mockedSong.one.more.time.mockReturnValue(12); diff --git a/docs/UpgradingToJest29.md b/docs/UpgradingToJest29.md index bf42cdc34555..6e429328a7d4 100644 --- a/docs/UpgradingToJest29.md +++ b/docs/UpgradingToJest29.md @@ -62,7 +62,7 @@ import {jest} from '@jest/globals'; ### `jest.mocked()` -The `mocked()` method now wraps deep members of passed object by default. If you used the method with `true` argument before, remove it to avoid type errors: +The `mocked()` helper method now wraps types of deep members of passed object by default. If you have used the method with `true` argument before, remove it to avoid type errors: ```diff - const mockedObject = jest.mocked(someObject, true); diff --git a/packages/jest-environment/src/index.ts b/packages/jest-environment/src/index.ts index f03a3615a119..4d4df64eaad7 100644 --- a/packages/jest-environment/src/index.ts +++ b/packages/jest-environment/src/index.ts @@ -202,8 +202,9 @@ export interface Jest { */ requireActual: (moduleName: string) => unknown; /** - * Wraps `source` object and its deep members with type definitions of Jest mock function. - * Pass `{shallow: true}` option to disable the deeply mocked behavior. + * Wraps types of the `source` object and its deep members with type definitions + * of Jest mock function. Pass `{shallow: true}` option to disable the deeply + * mocked behavior. */ mocked: ModuleMocker['mocked']; /** From f4000c02a1af65c0e76c3032f61fabaab96e1123 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Sat, 13 Aug 2022 11:04:33 +0300 Subject: [PATCH 06/12] clean up --- docs/MockFunctionAPI.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/MockFunctionAPI.md b/docs/MockFunctionAPI.md index 20f5b51653ce..1549ee9e0876 100644 --- a/docs/MockFunctionAPI.md +++ b/docs/MockFunctionAPI.md @@ -550,7 +550,7 @@ test('returns correct data', () => { Types of classes, functions or objects can be passed as type argument to `jest.Mocked`. If you prefer to constrain the input type, use: `jest.MockedClass`, `jest.MockedFunction` or `jest.MockedObject`. -### `jest.mocked(source: T, {shallow?: boolean})` +### `jest.mocked(source, {shallow?: boolean})` 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}` option to disable the deeply mocked behavior. From 243b81a6a43b1da75f183be3ca881e59a45cc73b Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Sat, 13 Aug 2022 11:04:46 +0300 Subject: [PATCH 07/12] add TS usage tip --- docs/JestObjectAPI.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/JestObjectAPI.md b/docs/JestObjectAPI.md index 072433a30176..88eeb4f8a864 100644 --- a/docs/JestObjectAPI.md +++ b/docs/JestObjectAPI.md @@ -282,6 +282,12 @@ Modules that are mocked with `jest.mock` are mocked only for the file that calls Returns the `jest` object for chaining. +:::tip + +Writing tests in TypeScript? Use [`jest.Mocked`](MockFunctionAPI.md/#jestmockedsource) utility type or [`jest.mocked()`](MockFunctionAPI.md/#jestmockedsource-shallow-boolean) helper method to have your mocked modules typed. + +::: + ### `jest.unmock(moduleName)` 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). @@ -467,7 +473,7 @@ const returnsTrue = jest.fn(() => true); console.log(returnsTrue()); // true; ``` -:::note +:::tip See [Mock Functions](MockFunctionAPI.md#jestfnimplementation) page for details on TypeScript usage. From 9240f850239f7800dfe4704903570f8459ca2009 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Sat, 13 Aug 2022 11:06:53 +0300 Subject: [PATCH 08/12] simplify example --- docs/MockFunctionAPI.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/MockFunctionAPI.md b/docs/MockFunctionAPI.md index 1549ee9e0876..ee2555fddf84 100644 --- a/docs/MockFunctionAPI.md +++ b/docs/MockFunctionAPI.md @@ -534,7 +534,7 @@ jest.mock('node-fetch'); let mockedFetch: jest.Mocked; afterEach(() => { - mockedFetch = resetMockedFetch(); + mockedFetch.mockClear(); }); test('makes correct call', () => { From 35c0b1b502b45739005ae33a29561814afdfc4f3 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Sat, 13 Aug 2022 11:08:23 +0300 Subject: [PATCH 09/12] clean up --- docs/MockFunctionAPI.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/MockFunctionAPI.md b/docs/MockFunctionAPI.md index ee2555fddf84..45719be62337 100644 --- a/docs/MockFunctionAPI.md +++ b/docs/MockFunctionAPI.md @@ -554,7 +554,7 @@ Types of classes, functions or objects can be passed as type argument to `jest.M 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}` option to disable the deeply mocked behavior. -Returns the input value. +Returns the `source` object. ```ts title="song.ts" export const song = { From abfe2c8dd70a6277b27cd05a856c24e3ace1f4a1 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Sat, 13 Aug 2022 13:05:51 +0300 Subject: [PATCH 10/12] add headings and polish few details --- docs/JestObjectAPI.md | 10 +++++++++- docs/MockFunctionAPI.md | 4 ++-- docs/UpgradingToJest29.md | 4 ++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/docs/JestObjectAPI.md b/docs/JestObjectAPI.md index 88eeb4f8a864..add6b8c49a6c 100644 --- a/docs/JestObjectAPI.md +++ b/docs/JestObjectAPI.md @@ -284,10 +284,18 @@ Returns the `jest` object for chaining. :::tip -Writing tests in TypeScript? Use [`jest.Mocked`](MockFunctionAPI.md/#jestmockedsource) utility type or [`jest.mocked()`](MockFunctionAPI.md/#jestmockedsource-shallow-boolean) helper method to have your mocked modules typed. +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. ::: +### `jest.Mocked` + +See [TypeScript Usage](MockFunctionAPI.md/#jestmockedsource) chapter of Mock Functions page for documentation. + +### `jest.mocked(source, options?)` + +See [TypeScript Usage](MockFunctionAPI.md/#jestmockedsource-options) chapter of Mock Functions page for documentation. + ### `jest.unmock(moduleName)` 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). diff --git a/docs/MockFunctionAPI.md b/docs/MockFunctionAPI.md index 45719be62337..db5b5b032aa1 100644 --- a/docs/MockFunctionAPI.md +++ b/docs/MockFunctionAPI.md @@ -550,9 +550,9 @@ test('returns correct data', () => { Types of classes, functions or objects can be passed as type argument to `jest.Mocked`. If you prefer to constrain the input type, use: `jest.MockedClass`, `jest.MockedFunction` or `jest.MockedObject`. -### `jest.mocked(source, {shallow?: boolean})` +### `jest.mocked(source, options?)` -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}` option to disable the deeply mocked behavior. +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. Returns the `source` object. diff --git a/docs/UpgradingToJest29.md b/docs/UpgradingToJest29.md index 6e429328a7d4..89dacba4beca 100644 --- a/docs/UpgradingToJest29.md +++ b/docs/UpgradingToJest29.md @@ -62,14 +62,14 @@ import {jest} from '@jest/globals'; ### `jest.mocked()` -The `mocked()` helper method now wraps types of deep members of passed object by default. If you have used the method with `true` argument before, remove it to avoid type errors: +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: ```diff - const mockedObject = jest.mocked(someObject, true); + const mockedObject = jest.mocked(someObject); ``` -To have the old shallow mocked behavior, pass `{shallow: true}` option: +To have the old shallow mocked behavior, pass `{shallow: true}` as the second argument: ```diff - const mockedObject = jest.mocked(someObject); From 6940a30a518b2ae8cb80a20a82fce710b3814a6d Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Sat, 13 Aug 2022 13:08:46 +0300 Subject: [PATCH 11/12] bring back node-fetch --- docs/MockFunctionAPI.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/MockFunctionAPI.md b/docs/MockFunctionAPI.md index db5b5b032aa1..06f5f0848994 100644 --- a/docs/MockFunctionAPI.md +++ b/docs/MockFunctionAPI.md @@ -526,8 +526,8 @@ test('calculate calls add', () => { The `jest.Mocked` utility type returns the `Source` type wrapped with type definitions of Jest mock function. ```ts +import type {fetch} from 'node-fetch'; import {expect, jest, test} from '@jest/globals'; -import type {fetch} from './fetch'; jest.mock('node-fetch'); From 63a0a5aa199909864a7b978fd728c56f0d0b3988 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Sat, 13 Aug 2022 13:11:05 +0300 Subject: [PATCH 12/12] fix order --- .eslintrc.cjs | 2 +- docs/MockFunctionAPI.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 683a6424e1ac..3a445ce334a1 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -180,7 +180,7 @@ module.exports = { }, }, { - files: ['website/**/*'], + files: ['docs/**/*', 'website/**/*'], rules: { 'import/order': 'off', 'import/sort-keys': 'off', diff --git a/docs/MockFunctionAPI.md b/docs/MockFunctionAPI.md index 06f5f0848994..526110167a84 100644 --- a/docs/MockFunctionAPI.md +++ b/docs/MockFunctionAPI.md @@ -526,8 +526,8 @@ test('calculate calls add', () => { The `jest.Mocked` utility type returns the `Source` type wrapped with type definitions of Jest mock function. ```ts -import type {fetch} from 'node-fetch'; import {expect, jest, test} from '@jest/globals'; +import type {fetch} from 'node-fetch'; jest.mock('node-fetch');