Skip to content

Commit 9ebb373

Browse files
authored
docs: fix one of mockFn.mockImplementation() TS examples (#13775)
1 parent 86f3c89 commit 9ebb373

File tree

6 files changed

+108
-6
lines changed

6 files changed

+108
-6
lines changed

docs/MockFunctionAPI.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ mockFn(3); // 39
163163
```
164164

165165
```ts tab
166+
import {jest} from '@jest/globals';
167+
166168
const mockFn = jest.fn((scalar: number) => 42 + scalar);
167169

168170
mockFn(0); // 42
@@ -207,12 +209,13 @@ export class SomeClass {
207209
```
208210

209211
```ts title="SomeClass.test.ts"
212+
import {jest} from '@jest/globals';
210213
import {SomeClass} from './SomeClass';
211214

212215
jest.mock('./SomeClass'); // this happens automatically with automocking
213216

214217
const mockMethod = jest.fn<(a: string, b: string) => void>();
215-
SomeClass.mockImplementation(() => {
218+
jest.mocked(SomeClass).mockImplementation(() => {
216219
return {
217220
method: mockMethod,
218221
};
@@ -239,6 +242,8 @@ mockFn((err, val) => console.log(val)); // false
239242
```
240243

241244
```ts tab
245+
import {jest} from '@jest/globals';
246+
242247
const mockFn = jest
243248
.fn<(cb: (a: null, b: boolean) => void) => void>()
244249
.mockImplementationOnce(cb => cb(null, true))
@@ -315,6 +320,8 @@ mock(); // 43
315320
```
316321

317322
```ts tab
323+
import {jest} from '@jest/globals';
324+
318325
const mock = jest.fn<() => number>();
319326

320327
mock.mockReturnValue(42);
@@ -348,6 +355,8 @@ mockFn(); // 'default'
348355
```
349356

350357
```ts tab
358+
import {jest} from '@jest/globals';
359+
351360
const mockFn = jest
352361
.fn<() => string>()
353362
.mockReturnValue('default')
@@ -379,6 +388,8 @@ test('async test', async () => {
379388
```
380389

381390
```ts tab
391+
import {jest, test} from '@jest/globals';
392+
382393
test('async test', async () => {
383394
const asyncMock = jest.fn<() => Promise<number>>().mockResolvedValue(43);
384395

@@ -412,6 +423,8 @@ test('async test', async () => {
412423
```
413424

414425
```ts tab
426+
import {jest, test} from '@jest/globals';
427+
415428
test('async test', async () => {
416429
const asyncMock = jest
417430
.fn<() => Promise<string>>()
@@ -447,6 +460,8 @@ test('async test', async () => {
447460
```
448461

449462
```ts tab
463+
import {jest, test} from '@jest/globals';
464+
450465
test('async test', async () => {
451466
const asyncMock = jest
452467
.fn<() => Promise<never>>()
@@ -479,6 +494,8 @@ test('async test', async () => {
479494
```
480495

481496
```ts tab
497+
import {jest, test} from '@jest/globals';
498+
482499
test('async test', async () => {
483500
const asyncMock = jest
484501
.fn<() => Promise<string>>()

website/versioned_docs/version-28.x/MockFunctionAPI.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ mockFn(3); // 39
163163
```
164164

165165
```ts tab
166+
import {jest} from '@jest/globals';
167+
166168
const mockFn = jest.fn((scalar: number) => 42 + scalar);
167169

168170
mockFn(0); // 42
@@ -207,12 +209,13 @@ export class SomeClass {
207209
```
208210

209211
```ts title="SomeClass.test.ts"
212+
import {jest} from '@jest/globals';
210213
import {SomeClass} from './SomeClass';
211214

212215
jest.mock('./SomeClass'); // this happens automatically with automocking
213216

214217
const mockMethod = jest.fn<(a: string, b: string) => void>();
215-
SomeClass.mockImplementation(() => {
218+
jest.mocked(SomeClass).mockImplementation(() => {
216219
return {
217220
method: mockMethod,
218221
};
@@ -239,6 +242,8 @@ mockFn((err, val) => console.log(val)); // false
239242
```
240243

241244
```ts tab
245+
import {jest} from '@jest/globals';
246+
242247
const mockFn = jest
243248
.fn<(cb: (a: null, b: boolean) => void) => void>()
244249
.mockImplementationOnce(cb => cb(null, true))
@@ -315,6 +320,8 @@ mock(); // 43
315320
```
316321

317322
```ts tab
323+
import {jest} from '@jest/globals';
324+
318325
const mock = jest.fn<() => number>();
319326

320327
mock.mockReturnValue(42);
@@ -348,6 +355,8 @@ mockFn(); // 'default'
348355
```
349356

350357
```ts tab
358+
import {jest} from '@jest/globals';
359+
351360
const mockFn = jest
352361
.fn<() => string>()
353362
.mockReturnValue('default')
@@ -379,6 +388,8 @@ test('async test', async () => {
379388
```
380389

381390
```ts tab
391+
import {jest, test} from '@jest/globals';
392+
382393
test('async test', async () => {
383394
const asyncMock = jest.fn<() => Promise<number>>().mockResolvedValue(43);
384395

@@ -412,6 +423,8 @@ test('async test', async () => {
412423
```
413424

414425
```ts tab
426+
import {jest, test} from '@jest/globals';
427+
415428
test('async test', async () => {
416429
const asyncMock = jest
417430
.fn<() => Promise<string>>()
@@ -447,6 +460,8 @@ test('async test', async () => {
447460
```
448461

449462
```ts tab
463+
import {jest, test} from '@jest/globals';
464+
450465
test('async test', async () => {
451466
const asyncMock = jest
452467
.fn<() => Promise<never>>()
@@ -479,6 +494,8 @@ test('async test', async () => {
479494
```
480495

481496
```ts tab
497+
import {jest, test} from '@jest/globals';
498+
482499
test('async test', async () => {
483500
const asyncMock = jest
484501
.fn<() => Promise<string>>()

website/versioned_docs/version-29.0/MockFunctionAPI.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ mockFn(3); // 39
163163
```
164164

165165
```ts tab
166+
import {jest} from '@jest/globals';
167+
166168
const mockFn = jest.fn((scalar: number) => 42 + scalar);
167169

168170
mockFn(0); // 42
@@ -207,12 +209,13 @@ export class SomeClass {
207209
```
208210

209211
```ts title="SomeClass.test.ts"
212+
import {jest} from '@jest/globals';
210213
import {SomeClass} from './SomeClass';
211214

212215
jest.mock('./SomeClass'); // this happens automatically with automocking
213216

214217
const mockMethod = jest.fn<(a: string, b: string) => void>();
215-
SomeClass.mockImplementation(() => {
218+
jest.mocked(SomeClass).mockImplementation(() => {
216219
return {
217220
method: mockMethod,
218221
};
@@ -239,6 +242,8 @@ mockFn((err, val) => console.log(val)); // false
239242
```
240243

241244
```ts tab
245+
import {jest} from '@jest/globals';
246+
242247
const mockFn = jest
243248
.fn<(cb: (a: null, b: boolean) => void) => void>()
244249
.mockImplementationOnce(cb => cb(null, true))
@@ -315,6 +320,8 @@ mock(); // 43
315320
```
316321

317322
```ts tab
323+
import {jest} from '@jest/globals';
324+
318325
const mock = jest.fn<() => number>();
319326

320327
mock.mockReturnValue(42);
@@ -348,6 +355,8 @@ mockFn(); // 'default'
348355
```
349356

350357
```ts tab
358+
import {jest} from '@jest/globals';
359+
351360
const mockFn = jest
352361
.fn<() => string>()
353362
.mockReturnValue('default')
@@ -379,6 +388,8 @@ test('async test', async () => {
379388
```
380389

381390
```ts tab
391+
import {jest, test} from '@jest/globals';
392+
382393
test('async test', async () => {
383394
const asyncMock = jest.fn<() => Promise<number>>().mockResolvedValue(43);
384395

@@ -412,6 +423,8 @@ test('async test', async () => {
412423
```
413424

414425
```ts tab
426+
import {jest, test} from '@jest/globals';
427+
415428
test('async test', async () => {
416429
const asyncMock = jest
417430
.fn<() => Promise<string>>()
@@ -447,6 +460,8 @@ test('async test', async () => {
447460
```
448461

449462
```ts tab
463+
import {jest, test} from '@jest/globals';
464+
450465
test('async test', async () => {
451466
const asyncMock = jest
452467
.fn<() => Promise<never>>()
@@ -479,6 +494,8 @@ test('async test', async () => {
479494
```
480495

481496
```ts tab
497+
import {jest, test} from '@jest/globals';
498+
482499
test('async test', async () => {
483500
const asyncMock = jest
484501
.fn<() => Promise<string>>()

website/versioned_docs/version-29.1/MockFunctionAPI.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ mockFn(3); // 39
163163
```
164164

165165
```ts tab
166+
import {jest} from '@jest/globals';
167+
166168
const mockFn = jest.fn((scalar: number) => 42 + scalar);
167169

168170
mockFn(0); // 42
@@ -207,12 +209,13 @@ export class SomeClass {
207209
```
208210

209211
```ts title="SomeClass.test.ts"
212+
import {jest} from '@jest/globals';
210213
import {SomeClass} from './SomeClass';
211214

212215
jest.mock('./SomeClass'); // this happens automatically with automocking
213216

214217
const mockMethod = jest.fn<(a: string, b: string) => void>();
215-
SomeClass.mockImplementation(() => {
218+
jest.mocked(SomeClass).mockImplementation(() => {
216219
return {
217220
method: mockMethod,
218221
};
@@ -239,6 +242,8 @@ mockFn((err, val) => console.log(val)); // false
239242
```
240243

241244
```ts tab
245+
import {jest} from '@jest/globals';
246+
242247
const mockFn = jest
243248
.fn<(cb: (a: null, b: boolean) => void) => void>()
244249
.mockImplementationOnce(cb => cb(null, true))
@@ -315,6 +320,8 @@ mock(); // 43
315320
```
316321

317322
```ts tab
323+
import {jest} from '@jest/globals';
324+
318325
const mock = jest.fn<() => number>();
319326

320327
mock.mockReturnValue(42);
@@ -348,6 +355,8 @@ mockFn(); // 'default'
348355
```
349356

350357
```ts tab
358+
import {jest} from '@jest/globals';
359+
351360
const mockFn = jest
352361
.fn<() => string>()
353362
.mockReturnValue('default')
@@ -379,6 +388,8 @@ test('async test', async () => {
379388
```
380389

381390
```ts tab
391+
import {jest, test} from '@jest/globals';
392+
382393
test('async test', async () => {
383394
const asyncMock = jest.fn<() => Promise<number>>().mockResolvedValue(43);
384395

@@ -412,6 +423,8 @@ test('async test', async () => {
412423
```
413424

414425
```ts tab
426+
import {jest, test} from '@jest/globals';
427+
415428
test('async test', async () => {
416429
const asyncMock = jest
417430
.fn<() => Promise<string>>()
@@ -447,6 +460,8 @@ test('async test', async () => {
447460
```
448461

449462
```ts tab
463+
import {jest, test} from '@jest/globals';
464+
450465
test('async test', async () => {
451466
const asyncMock = jest
452467
.fn<() => Promise<never>>()
@@ -479,6 +494,8 @@ test('async test', async () => {
479494
```
480495

481496
```ts tab
497+
import {jest, test} from '@jest/globals';
498+
482499
test('async test', async () => {
483500
const asyncMock = jest
484501
.fn<() => Promise<string>>()

0 commit comments

Comments
 (0)