Skip to content

Commit 4b6a4f2

Browse files
MillerSvts.v.zaytsevG-Rath
authored
feat(prefer-jest-mocked): add new rule (#1599)
Co-authored-by: s.v.zaytsev <[email protected]> Co-authored-by: Gareth Jones <[email protected]>
1 parent 5b9b47e commit 4b6a4f2

File tree

6 files changed

+460
-1
lines changed

6 files changed

+460
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ Manually fixable by
355355
| [prefer-hooks-in-order](docs/rules/prefer-hooks-in-order.md) | Prefer having hooks in a consistent order | | | | |
356356
| [prefer-hooks-on-top](docs/rules/prefer-hooks-on-top.md) | Suggest having hooks before any test cases | | | | |
357357
| [prefer-importing-jest-globals](docs/rules/prefer-importing-jest-globals.md) | Prefer importing Jest globals | | | 🔧 | |
358+
| [prefer-jest-mocked](docs/rules/prefer-jest-mocked.md) | Prefer `jest.mocked()` over `fn as jest.Mock` | | | 🔧 | |
358359
| [prefer-lowercase-title](docs/rules/prefer-lowercase-title.md) | Enforce lowercase test names | | | 🔧 | |
359360
| [prefer-mock-promise-shorthand](docs/rules/prefer-mock-promise-shorthand.md) | Prefer mock resolved/rejected shorthands for promises | | | 🔧 | |
360361
| [prefer-snapshot-hint](docs/rules/prefer-snapshot-hint.md) | Prefer including a hint with external snapshots | | | | |

docs/rules/prefer-jest-mocked.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Prefer `jest.mocked()` over `fn as jest.Mock` (`prefer-jest-mocked`)
2+
3+
🔧 This rule is automatically fixable by the
4+
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
5+
6+
<!-- end auto-generated rule header -->
7+
8+
When working with mocks of functions using Jest, it's recommended to use the
9+
`jest.mocked()` helper function to properly type the mocked functions. This rule
10+
enforces the use of `jest.mocked()` for better type safety and readability.
11+
12+
Restricted types:
13+
14+
- `jest.Mock`
15+
- `jest.MockedFunction`
16+
- `jest.MockedClass`
17+
- `jest.MockedObject`
18+
19+
## Rule details
20+
21+
The following patterns are warnings:
22+
23+
```typescript
24+
(foo as jest.Mock).mockReturnValue(1);
25+
const mock = (foo as jest.Mock).mockReturnValue(1);
26+
(foo as unknown as jest.Mock).mockReturnValue(1);
27+
(Obj.foo as jest.Mock).mockReturnValue(1);
28+
([].foo as jest.Mock).mockReturnValue(1);
29+
```
30+
31+
The following patterns are not warnings:
32+
33+
```js
34+
jest.mocked(foo).mockReturnValue(1);
35+
const mock = jest.mocked(foo).mockReturnValue(1);
36+
jest.mocked(Obj.foo).mockReturnValue(1);
37+
jest.mocked([].foo).mockReturnValue(1);
38+
```

src/__tests__/__snapshots__/rules.test.ts.snap

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ exports[`rules should export configs that refer to actual rules 1`] = `
4646
"jest/prefer-hooks-in-order": "error",
4747
"jest/prefer-hooks-on-top": "error",
4848
"jest/prefer-importing-jest-globals": "error",
49+
"jest/prefer-jest-mocked": "error",
4950
"jest/prefer-lowercase-title": "error",
5051
"jest/prefer-mock-promise-shorthand": "error",
5152
"jest/prefer-snapshot-hint": "error",
@@ -128,6 +129,7 @@ exports[`rules should export configs that refer to actual rules 1`] = `
128129
"jest/prefer-hooks-in-order": "error",
129130
"jest/prefer-hooks-on-top": "error",
130131
"jest/prefer-importing-jest-globals": "error",
132+
"jest/prefer-jest-mocked": "error",
131133
"jest/prefer-lowercase-title": "error",
132134
"jest/prefer-mock-promise-shorthand": "error",
133135
"jest/prefer-snapshot-hint": "error",

src/__tests__/rules.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { existsSync } from 'fs';
22
import { resolve } from 'path';
33
import plugin from '../';
44

5-
const numberOfRules = 53;
5+
const numberOfRules = 54;
66
const ruleNames = Object.keys(plugin.rules);
77
const deprecatedRules = Object.entries(plugin.rules)
88
.filter(([, rule]) => rule.meta.deprecated)

0 commit comments

Comments
 (0)