> For AI agents: the complete documentation index is available at https://rslint.rs/llms.txt, the full documentation bundle is available at https://rslint.rs/llms-full.txt.

# prefer-jest-mocked

[Added in v0.6.4](https://github.com/web-infra-dev/rslint/releases/tag/v0.6.4)
## Configuration

| Preset                   | Configured Value |
| ------------------------ | ---------------- |
| ✅ jestPlugin.configs.all | `"error"`        |


```ts title=rslint.config.ts
import { defineConfig, jestPlugin } from '@rslint/core';

export default defineConfig([
  jestPlugin.configs.recommended,
  {
    rules: {
      'jest/prefer-jest-mocked': 'error',
    },
  },
]);
```

## Rule Details

Prefer `jest.mocked()` over type assertions such as `fn as jest.Mock` when
working with mocked functions in Jest. The `jest.mocked()` helper preserves
correct mock typings without manual casts, which improves type safety and keeps
mock setup easier to read.

This rule reports type assertions and angle-bracket casts to Jest mock types,
including chained assertions like `as unknown as jest.Mock`. It is fixable.

Restricted types:

- `jest.Mock`
- `jest.MockedFunction`
- `jest.MockedClass`
- `jest.MockedObject`

Examples of **incorrect** code for this rule:

```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);
```

Examples of **correct** code for this rule:

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

## Original Documentation

- [eslint-plugin-jest: prefer-jest-mocked](https://github.com/jest-community/eslint-plugin-jest/blob/v29.16.0/docs/rules/prefer-jest-mocked.md)
- [Source code](https://github.com/jest-community/eslint-plugin-jest/blob/v29.16.0/src/rules/prefer-jest-mocked.ts)
