Skip to content

Commit 81f455f

Browse files
author
s.v.zaytsev
committed
feat(prefer-mocked): add new rule (jest-community#1470)
1 parent 70c8c5e commit 81f455f

File tree

6 files changed

+478
-1
lines changed

6 files changed

+478
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ set to warn in.\
352352
| [prefer-importing-jest-globals](docs/rules/prefer-importing-jest-globals.md) | Prefer importing Jest globals | | | 🔧 | |
353353
| [prefer-lowercase-title](docs/rules/prefer-lowercase-title.md) | Enforce lowercase test names | | | 🔧 | |
354354
| [prefer-mock-promise-shorthand](docs/rules/prefer-mock-promise-shorthand.md) | Prefer mock resolved/rejected shorthands for promises | | | 🔧 | |
355+
| [prefer-mocked](docs/rules/prefer-mocked.md) | Prefer jest.mocked() over (fn as jest.Mock) | | | 🔧 | |
355356
| [prefer-snapshot-hint](docs/rules/prefer-snapshot-hint.md) | Prefer including a hint with external snapshots | | | | |
356357
| [prefer-spy-on](docs/rules/prefer-spy-on.md) | Suggest using `jest.spyOn()` | | | 🔧 | |
357358
| [prefer-strict-equal](docs/rules/prefer-strict-equal.md) | Suggest using `toStrictEqual()` | | | | 💡 |

docs/rules/prefer-mocked.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Prefer jest.mocked() over (fn as jest.Mock) (`prefer-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+
## Rule details
13+
14+
The following patterns are warnings:
15+
16+
```typescript
17+
(foo as jest.Mock).mockReturnValue(1);
18+
const mock = (foo as jest.Mock).mockReturnValue(1);
19+
(foo as unknown as jest.Mock).mockReturnValue(1);
20+
(Obj.foo as jest.Mock).mockReturnValue(1);
21+
([].foo as jest.Mock).mockReturnValue(1);
22+
```
23+
24+
The following patterns are not warnings:
25+
26+
```js
27+
jest.mocked(foo).mockReturnValue(1);
28+
const mock = jest.mocked(foo).mockReturnValue(1);
29+
jest.mocked(Obj.foo).mockReturnValue(1);
30+
jest.mocked([].foo).mockReturnValue(1);
31+
```

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

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ exports[`rules should export configs that refer to actual rules 1`] = `
4848
"jest/prefer-importing-jest-globals": "error",
4949
"jest/prefer-lowercase-title": "error",
5050
"jest/prefer-mock-promise-shorthand": "error",
51+
"jest/prefer-mocked": "error",
5152
"jest/prefer-snapshot-hint": "error",
5253
"jest/prefer-spy-on": "error",
5354
"jest/prefer-strict-equal": "error",
@@ -130,6 +131,7 @@ exports[`rules should export configs that refer to actual rules 1`] = `
130131
"jest/prefer-importing-jest-globals": "error",
131132
"jest/prefer-lowercase-title": "error",
132133
"jest/prefer-mock-promise-shorthand": "error",
134+
"jest/prefer-mocked": "error",
133135
"jest/prefer-snapshot-hint": "error",
134136
"jest/prefer-spy-on": "error",
135137
"jest/prefer-strict-equal": "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)