# jest/prefer-mock-promise-shorthand

> Require `mockResolvedValue()` and `mockRejectedValue()` for mocks that return a promise

`mockReturnValue(Promise.resolve(user))` and `mockImplementation(() => Promise.resolve(user))` do the same thing as `mockResolvedValue(user)` with more code. The same goes for `Promise.reject()` and `mockRejectedValue()`, and for the `Once` version of each method.

`--fix` rewrites the call.

{/* 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-mock-promise-shorthand" = "error" # or "warn"
```

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

## Examples

Reported:

{/* bad {"count": 2} */}
```ts
import { jest } from "@jest/globals";

const fetchUser = jest.fn().mockReturnValue(Promise.resolve({ name: "Ada" }));
const saveUser = jest.fn().mockImplementationOnce(() => Promise.reject(new Error("offline")));
```

Not reported:

{/* good */}
```ts
import { jest } from "@jest/globals";

const fetchUser = jest.fn().mockResolvedValue({ name: "Ada" });
const saveUser = jest.fn().mockRejectedValueOnce(new Error("offline"));
```

An implementation that takes parameters is allowed, because its result can depend on them:

{/* good */}
```ts
import { jest } from "@jest/globals";

const fetchUser = jest.fn().mockImplementation((id: number) => Promise.resolve({ id }));
```
