File tree Expand file tree Collapse file tree 6 files changed +460
-1
lines changed Expand file tree Collapse file tree 6 files changed +460
-1
lines changed Original file line number Diff line number Diff 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 | | | | |
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`] = `
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" ,
Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ import { existsSync } from 'fs';
22import { resolve } from 'path' ;
33import plugin from '../' ;
44
5- const numberOfRules = 53 ;
5+ const numberOfRules = 54 ;
66const ruleNames = Object . keys ( plugin . rules ) ;
77const deprecatedRules = Object . entries ( plugin . rules )
88 . filter ( ( [ , rule ] ) => rule . meta . deprecated )
You can’t perform that action at this time.
0 commit comments