Skip to content

Commit

Permalink
docs: fix one of mockFn.mockImplementation() TS examples (#13775)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrazauskas authored Jan 17, 2023
1 parent 86f3c89 commit 9ebb373
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 6 deletions.
19 changes: 18 additions & 1 deletion docs/MockFunctionAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ mockFn(3); // 39
```

```ts tab
import {jest} from '@jest/globals';

const mockFn = jest.fn((scalar: number) => 42 + scalar);

mockFn(0); // 42
Expand Down Expand Up @@ -207,12 +209,13 @@ export class SomeClass {
```

```ts title="SomeClass.test.ts"
import {jest} from '@jest/globals';
import {SomeClass} from './SomeClass';

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

const mockMethod = jest.fn<(a: string, b: string) => void>();
SomeClass.mockImplementation(() => {
jest.mocked(SomeClass).mockImplementation(() => {
return {
method: mockMethod,
};
Expand All @@ -239,6 +242,8 @@ mockFn((err, val) => console.log(val)); // false
```

```ts tab
import {jest} from '@jest/globals';

const mockFn = jest
.fn<(cb: (a: null, b: boolean) => void) => void>()
.mockImplementationOnce(cb => cb(null, true))
Expand Down Expand Up @@ -315,6 +320,8 @@ mock(); // 43
```

```ts tab
import {jest} from '@jest/globals';

const mock = jest.fn<() => number>();

mock.mockReturnValue(42);
Expand Down Expand Up @@ -348,6 +355,8 @@ mockFn(); // 'default'
```

```ts tab
import {jest} from '@jest/globals';

const mockFn = jest
.fn<() => string>()
.mockReturnValue('default')
Expand Down Expand Up @@ -379,6 +388,8 @@ test('async test', async () => {
```

```ts tab
import {jest, test} from '@jest/globals';

test('async test', async () => {
const asyncMock = jest.fn<() => Promise<number>>().mockResolvedValue(43);

Expand Down Expand Up @@ -412,6 +423,8 @@ test('async test', async () => {
```

```ts tab
import {jest, test} from '@jest/globals';

test('async test', async () => {
const asyncMock = jest
.fn<() => Promise<string>>()
Expand Down Expand Up @@ -447,6 +460,8 @@ test('async test', async () => {
```

```ts tab
import {jest, test} from '@jest/globals';

test('async test', async () => {
const asyncMock = jest
.fn<() => Promise<never>>()
Expand Down Expand Up @@ -479,6 +494,8 @@ test('async test', async () => {
```

```ts tab
import {jest, test} from '@jest/globals';

test('async test', async () => {
const asyncMock = jest
.fn<() => Promise<string>>()
Expand Down
19 changes: 18 additions & 1 deletion website/versioned_docs/version-28.x/MockFunctionAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ mockFn(3); // 39
```

```ts tab
import {jest} from '@jest/globals';

const mockFn = jest.fn((scalar: number) => 42 + scalar);

mockFn(0); // 42
Expand Down Expand Up @@ -207,12 +209,13 @@ export class SomeClass {
```

```ts title="SomeClass.test.ts"
import {jest} from '@jest/globals';
import {SomeClass} from './SomeClass';

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

const mockMethod = jest.fn<(a: string, b: string) => void>();
SomeClass.mockImplementation(() => {
jest.mocked(SomeClass).mockImplementation(() => {
return {
method: mockMethod,
};
Expand All @@ -239,6 +242,8 @@ mockFn((err, val) => console.log(val)); // false
```

```ts tab
import {jest} from '@jest/globals';

const mockFn = jest
.fn<(cb: (a: null, b: boolean) => void) => void>()
.mockImplementationOnce(cb => cb(null, true))
Expand Down Expand Up @@ -315,6 +320,8 @@ mock(); // 43
```

```ts tab
import {jest} from '@jest/globals';

const mock = jest.fn<() => number>();

mock.mockReturnValue(42);
Expand Down Expand Up @@ -348,6 +355,8 @@ mockFn(); // 'default'
```

```ts tab
import {jest} from '@jest/globals';

const mockFn = jest
.fn<() => string>()
.mockReturnValue('default')
Expand Down Expand Up @@ -379,6 +388,8 @@ test('async test', async () => {
```

```ts tab
import {jest, test} from '@jest/globals';

test('async test', async () => {
const asyncMock = jest.fn<() => Promise<number>>().mockResolvedValue(43);

Expand Down Expand Up @@ -412,6 +423,8 @@ test('async test', async () => {
```

```ts tab
import {jest, test} from '@jest/globals';

test('async test', async () => {
const asyncMock = jest
.fn<() => Promise<string>>()
Expand Down Expand Up @@ -447,6 +460,8 @@ test('async test', async () => {
```

```ts tab
import {jest, test} from '@jest/globals';

test('async test', async () => {
const asyncMock = jest
.fn<() => Promise<never>>()
Expand Down Expand Up @@ -479,6 +494,8 @@ test('async test', async () => {
```

```ts tab
import {jest, test} from '@jest/globals';

test('async test', async () => {
const asyncMock = jest
.fn<() => Promise<string>>()
Expand Down
19 changes: 18 additions & 1 deletion website/versioned_docs/version-29.0/MockFunctionAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ mockFn(3); // 39
```

```ts tab
import {jest} from '@jest/globals';

const mockFn = jest.fn((scalar: number) => 42 + scalar);

mockFn(0); // 42
Expand Down Expand Up @@ -207,12 +209,13 @@ export class SomeClass {
```

```ts title="SomeClass.test.ts"
import {jest} from '@jest/globals';
import {SomeClass} from './SomeClass';

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

const mockMethod = jest.fn<(a: string, b: string) => void>();
SomeClass.mockImplementation(() => {
jest.mocked(SomeClass).mockImplementation(() => {
return {
method: mockMethod,
};
Expand All @@ -239,6 +242,8 @@ mockFn((err, val) => console.log(val)); // false
```

```ts tab
import {jest} from '@jest/globals';

const mockFn = jest
.fn<(cb: (a: null, b: boolean) => void) => void>()
.mockImplementationOnce(cb => cb(null, true))
Expand Down Expand Up @@ -315,6 +320,8 @@ mock(); // 43
```

```ts tab
import {jest} from '@jest/globals';

const mock = jest.fn<() => number>();

mock.mockReturnValue(42);
Expand Down Expand Up @@ -348,6 +355,8 @@ mockFn(); // 'default'
```

```ts tab
import {jest} from '@jest/globals';

const mockFn = jest
.fn<() => string>()
.mockReturnValue('default')
Expand Down Expand Up @@ -379,6 +388,8 @@ test('async test', async () => {
```

```ts tab
import {jest, test} from '@jest/globals';

test('async test', async () => {
const asyncMock = jest.fn<() => Promise<number>>().mockResolvedValue(43);

Expand Down Expand Up @@ -412,6 +423,8 @@ test('async test', async () => {
```

```ts tab
import {jest, test} from '@jest/globals';

test('async test', async () => {
const asyncMock = jest
.fn<() => Promise<string>>()
Expand Down Expand Up @@ -447,6 +460,8 @@ test('async test', async () => {
```

```ts tab
import {jest, test} from '@jest/globals';

test('async test', async () => {
const asyncMock = jest
.fn<() => Promise<never>>()
Expand Down Expand Up @@ -479,6 +494,8 @@ test('async test', async () => {
```

```ts tab
import {jest, test} from '@jest/globals';

test('async test', async () => {
const asyncMock = jest
.fn<() => Promise<string>>()
Expand Down
19 changes: 18 additions & 1 deletion website/versioned_docs/version-29.1/MockFunctionAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ mockFn(3); // 39
```

```ts tab
import {jest} from '@jest/globals';

const mockFn = jest.fn((scalar: number) => 42 + scalar);

mockFn(0); // 42
Expand Down Expand Up @@ -207,12 +209,13 @@ export class SomeClass {
```

```ts title="SomeClass.test.ts"
import {jest} from '@jest/globals';
import {SomeClass} from './SomeClass';

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

const mockMethod = jest.fn<(a: string, b: string) => void>();
SomeClass.mockImplementation(() => {
jest.mocked(SomeClass).mockImplementation(() => {
return {
method: mockMethod,
};
Expand All @@ -239,6 +242,8 @@ mockFn((err, val) => console.log(val)); // false
```

```ts tab
import {jest} from '@jest/globals';

const mockFn = jest
.fn<(cb: (a: null, b: boolean) => void) => void>()
.mockImplementationOnce(cb => cb(null, true))
Expand Down Expand Up @@ -315,6 +320,8 @@ mock(); // 43
```

```ts tab
import {jest} from '@jest/globals';

const mock = jest.fn<() => number>();

mock.mockReturnValue(42);
Expand Down Expand Up @@ -348,6 +355,8 @@ mockFn(); // 'default'
```

```ts tab
import {jest} from '@jest/globals';

const mockFn = jest
.fn<() => string>()
.mockReturnValue('default')
Expand Down Expand Up @@ -379,6 +388,8 @@ test('async test', async () => {
```

```ts tab
import {jest, test} from '@jest/globals';

test('async test', async () => {
const asyncMock = jest.fn<() => Promise<number>>().mockResolvedValue(43);

Expand Down Expand Up @@ -412,6 +423,8 @@ test('async test', async () => {
```

```ts tab
import {jest, test} from '@jest/globals';

test('async test', async () => {
const asyncMock = jest
.fn<() => Promise<string>>()
Expand Down Expand Up @@ -447,6 +460,8 @@ test('async test', async () => {
```

```ts tab
import {jest, test} from '@jest/globals';

test('async test', async () => {
const asyncMock = jest
.fn<() => Promise<never>>()
Expand Down Expand Up @@ -479,6 +494,8 @@ test('async test', async () => {
```

```ts tab
import {jest, test} from '@jest/globals';

test('async test', async () => {
const asyncMock = jest
.fn<() => Promise<string>>()
Expand Down
Loading

0 comments on commit 9ebb373

Please sign in to comment.