Commit 4b6a4f2 1 parent 5b9b47e commit 4b6a4f2 Copy full SHA for 4b6a4f2
File tree 6 files changed +460
-1
lines changed
6 files changed +460
-1
lines changed Original file line number Diff line number Diff line change @@ -355,6 +355,7 @@ Manually fixable by
355
355
| [ prefer-hooks-in-order] ( docs/rules/prefer-hooks-in-order.md ) | Prefer having hooks in a consistent order | | | | |
356
356
| [ prefer-hooks-on-top] ( docs/rules/prefer-hooks-on-top.md ) | Suggest having hooks before any test cases | | | | |
357
357
| [ 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 ` | | | 🔧 | |
358
359
| [ prefer-lowercase-title] ( docs/rules/prefer-lowercase-title.md ) | Enforce lowercase test names | | | 🔧 | |
359
360
| [ prefer-mock-promise-shorthand] ( docs/rules/prefer-mock-promise-shorthand.md ) | Prefer mock resolved/rejected shorthands for promises | | | 🔧 | |
360
361
| [ prefer-snapshot-hint] ( docs/rules/prefer-snapshot-hint.md ) | Prefer including a hint with external snapshots | | | | |
Original file line number Diff line number Diff line change
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
+ ```
Original file line number Diff line number Diff line change @@ -46,6 +46,7 @@ exports[`rules should export configs that refer to actual rules 1`] = `
46
46
" jest/prefer-hooks-in-order" : " error" ,
47
47
" jest/prefer-hooks-on-top" : " error" ,
48
48
" jest/prefer-importing-jest-globals" : " error" ,
49
+ " jest/prefer-jest-mocked" : " error" ,
49
50
" jest/prefer-lowercase-title" : " error" ,
50
51
" jest/prefer-mock-promise-shorthand" : " error" ,
51
52
" jest/prefer-snapshot-hint" : " error" ,
@@ -128,6 +129,7 @@ exports[`rules should export configs that refer to actual rules 1`] = `
128
129
" jest/prefer-hooks-in-order" : " error" ,
129
130
" jest/prefer-hooks-on-top" : " error" ,
130
131
" jest/prefer-importing-jest-globals" : " error" ,
132
+ " jest/prefer-jest-mocked" : " error" ,
131
133
" jest/prefer-lowercase-title" : " error" ,
132
134
" jest/prefer-mock-promise-shorthand" : " error" ,
133
135
" jest/prefer-snapshot-hint" : " 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