# vitest/prefer-mock-return-shorthand

> Require `mockReturnValue()` for a mock that only returns a value

`mockImplementation(() => value)` wraps a value in a function that only returns it. `mockReturnValue(value)` does the same thing with less code. The same goes for `mockImplementationOnce()` and `mockReturnValueOnce()`.

`--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]
"vitest/prefer-mock-return-shorthand" = "error" # or "warn"
```

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

## Examples

Reported:

{/* bad {"count": 2} */}
```ts
import { vi } from "vitest";

const getPort = vi.fn().mockImplementation(() => 3000);

const getUser = vi.fn().mockImplementationOnce(() => {
  return { name: "Ada" };
});
```

Not reported:

{/* good */}
```ts
import { vi } from "vitest";

const getPort = vi.fn().mockReturnValue(3000);

const getUser = vi.fn().mockReturnValueOnce({ name: "Ada" });
```

A function that takes parameters, is `async`, does more than return, or returns a `let` or `var` variable needs `mockImplementation()`, so it isn't reported:

{/* good */}
```ts
import { vi } from "vitest";

let visits = 0;

const double = vi.fn().mockImplementation((n: number) => n * 2);
const load = vi.fn().mockImplementation(async () => "done");
const countVisits = vi.fn().mockImplementation(() => visits);
const nextVisit = vi.fn().mockImplementation(() => {
  visits += 1;
  return "ok";
});
```

A function that returns the result of a call or a `new` is reported, but `--fix` leaves it alone. `mockReturnValue(createUser())` calls `createUser()` once, not each time the mock is called.

{/* bad */}
```ts
import { vi } from "vitest";

const getUser = vi.fn().mockImplementation(() => createUser());
```

## When not to use it

`mockReturnValue({ name: "Ada" })` returns the same object on every call. `mockImplementation(() => ({ name: "Ada" }))` returns a new one. If the code under test changes what the mock returns, keep the function and disable the rule on that line.
