# vitest/prefer-expect-resolves

> Require `await expect(promise).resolves` instead of `expect(await promise)`

In `expect(await promise)`, a rejected promise throws before `expect` is called, so the test fails with the raw error. With `.resolves`, `expect` gets the promise, and a rejection is reported as a failed assertion.

`--fix` rewrites the assertion.

{/* 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-expect-resolves" = "error" # or "warn"
```

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

## Examples

Reported:

{/* bad */}
```ts
import { expect, test } from "vitest";

test("loads the user", async () => {
  expect(await loadUser(1)).toEqual({ id: 1, name: "Ada" });
});
```

Not reported:

{/* good */}
```ts
import { expect, test } from "vitest";

test("loads the user", async () => {
  await expect(loadUser(1)).resolves.toEqual({ id: 1, name: "Ada" });
});

test("loads the user and checks two fields", async () => {
  const user = await loadUser(1);
  expect(user.id).toBe(1);
  expect(user.name).toBe("Ada");
});
```

## When not to use it

`await` accepts any value, but `.resolves` fails if it doesn't get a promise. If a function returns a plain value in some cases, keep `expect(await ...)` there.
