# jest/prefer-jest-mocked

> Require `jest.mocked(fn)` instead of `fn as jest.Mock`

`fetchUser as jest.Mock` replaces the function's type with a mock of anything, so `mockReturnValue` accepts a value that the real function can never return. `jest.mocked(fetchUser)` keeps the parameters and the return type, so a mock value with the wrong shape is a type error.

The rule reports a cast to `jest.Mock`, `jest.MockedFunction`, `jest.MockedClass` or `jest.MockedObject`, with or without type arguments.

`--fix` rewrites the cast to `jest.mocked()`.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

```toml bunfig.toml
[lint.rules]
"jest/prefer-jest-mocked" = "error" # or "warn"
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 2} */}
```ts
import { jest } from "@jest/globals";
import { fetchUser, saveUser } from "./api";

jest.mock("./api");

(fetchUser as jest.Mock).mockReturnValue({ name: "Ada" });
const mockSaveUser = saveUser as jest.MockedFunction<typeof saveUser>;
```

Not reported:

{/* good */}
```ts
import { jest } from "@jest/globals";
import { fetchUser, saveUser } from "./api";

jest.mock("./api");

jest.mocked(fetchUser).mockReturnValue({ name: "Ada" });
const mockSaveUser = jest.mocked(saveUser);
```
