Skip to content

Latest commit

 

History

History
38 lines (28 loc) · 1.08 KB

prefer-jest-mocked.md

File metadata and controls

38 lines (28 loc) · 1.08 KB

Prefer jest.mocked() over fn as jest.Mock (prefer-jest-mocked)

🔧 This rule is automatically fixable by the --fix CLI option.

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:

(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:

jest.mocked(foo).mockReturnValue(1);
const mock = jest.mocked(foo).mockReturnValue(1);
jest.mocked(Obj.foo).mockReturnValue(1);
jest.mocked([].foo).mockReturnValue(1);