File tree 6 files changed +522
-1
lines changed
6 files changed +522
-1
lines changed Original file line number Diff line number Diff line change @@ -352,6 +352,7 @@ set to warn in.\
352
352
| [ prefer-importing-jest-globals] ( docs/rules/prefer-importing-jest-globals.md ) | Prefer importing Jest globals | | | 🔧 | |
353
353
| [ prefer-lowercase-title] ( docs/rules/prefer-lowercase-title.md ) | Enforce lowercase test names | | | 🔧 | |
354
354
| [ 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) | | | 🔧 | |
355
356
| [ prefer-snapshot-hint] ( docs/rules/prefer-snapshot-hint.md ) | Prefer including a hint with external snapshots | | | | |
356
357
| [ prefer-spy-on] ( docs/rules/prefer-spy-on.md ) | Suggest using ` jest.spyOn() ` | | | 🔧 | |
357
358
| [ prefer-strict-equal] ( docs/rules/prefer-strict-equal.md ) | Suggest using ` toStrictEqual() ` | | | | 💡 |
Original file line number Diff line number Diff line change
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
+ Restricted types:
13
+ * ` jest.Mock `
14
+ * ` jest.MockedFunction `
15
+ * ` jest.MockedClass `
16
+ * ` jest.MockedObject `
17
+
18
+ ## Rule details
19
+
20
+ The following patterns are warnings:
21
+
22
+ ``` typescript
23
+ (foo as jest .Mock ).mockReturnValue (1 );
24
+ const mock = (foo as jest .Mock ).mockReturnValue (1 );
25
+ (foo as unknown as jest .Mock ).mockReturnValue (1 );
26
+ (Obj .foo as jest .Mock ).mockReturnValue (1 );
27
+ ([].foo as jest .Mock ).mockReturnValue (1 );
28
+ ```
29
+
30
+ The following patterns are not warnings:
31
+
32
+ ``` js
33
+ jest .mocked (foo).mockReturnValue (1 );
34
+ const mock = jest .mocked (foo).mockReturnValue (1 );
35
+ jest .mocked (Obj .foo ).mockReturnValue (1 );
36
+ jest .mocked ([].foo ).mockReturnValue (1 );
37
+ ```
Original file line number Diff line number Diff line change @@ -48,6 +48,7 @@ exports[`rules should export configs that refer to actual rules 1`] = `
48
48
" jest/prefer-importing-jest-globals" : " error" ,
49
49
" jest/prefer-lowercase-title" : " error" ,
50
50
" jest/prefer-mock-promise-shorthand" : " error" ,
51
+ " jest/prefer-mocked" : " error" ,
51
52
" jest/prefer-snapshot-hint" : " error" ,
52
53
" jest/prefer-spy-on" : " error" ,
53
54
" jest/prefer-strict-equal" : " error" ,
@@ -130,6 +131,7 @@ exports[`rules should export configs that refer to actual rules 1`] = `
130
131
" jest/prefer-importing-jest-globals" : " error" ,
131
132
" jest/prefer-lowercase-title" : " error" ,
132
133
" jest/prefer-mock-promise-shorthand" : " error" ,
134
+ " jest/prefer-mocked" : " error" ,
133
135
" jest/prefer-snapshot-hint" : " error" ,
134
136
" jest/prefer-spy-on" : " error" ,
135
137
" jest/prefer-strict-equal" : " error" ,
Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ import { existsSync } from 'fs';
2
2
import { resolve } from 'path' ;
3
3
import plugin from '../' ;
4
4
5
- const numberOfRules = 53 ;
5
+ const numberOfRules = 54 ;
6
6
const ruleNames = Object . keys ( plugin . rules ) ;
7
7
const deprecatedRules = Object . entries ( plugin . rules )
8
8
. filter ( ( [ , rule ] ) => rule . meta . deprecated )
You can’t perform that action at this time.
0 commit comments