-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(prefer-jest-mocked): add new rule (#1599)
Co-authored-by: s.v.zaytsev <s.v.zaytsev@tinkoff.ru> Co-authored-by: Gareth Jones <Jones258@Gmail.com>
- Loading branch information
1 parent
5b9b47e
commit 4b6a4f2
Showing
6 changed files
with
460 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Prefer `jest.mocked()` over `fn as jest.Mock` (`prefer-jest-mocked`) | ||
|
||
🔧 This rule is automatically fixable by the | ||
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
When working with mocks of functions using Jest, it's recommended to use the | ||
`jest.mocked()` helper function to properly type the mocked functions. This rule | ||
enforces the use of `jest.mocked()` for better type safety and readability. | ||
|
||
Restricted types: | ||
|
||
- `jest.Mock` | ||
- `jest.MockedFunction` | ||
- `jest.MockedClass` | ||
- `jest.MockedObject` | ||
|
||
## Rule details | ||
|
||
The following patterns are warnings: | ||
|
||
```typescript | ||
(foo as jest.Mock).mockReturnValue(1); | ||
const mock = (foo as jest.Mock).mockReturnValue(1); | ||
(foo as unknown as jest.Mock).mockReturnValue(1); | ||
(Obj.foo as jest.Mock).mockReturnValue(1); | ||
([].foo as jest.Mock).mockReturnValue(1); | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```js | ||
jest.mocked(foo).mockReturnValue(1); | ||
const mock = jest.mocked(foo).mockReturnValue(1); | ||
jest.mocked(Obj.foo).mockReturnValue(1); | ||
jest.mocked([].foo).mockReturnValue(1); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.