r/javascript • • 21d ago

AskJS [AskJS] How do you keep un-awaited expect(...).resolves out of your test suites?

I've been reading test suites in open-source repos this month and kept running into this:

it('saves the user', async () => {
  expect(save(user)).resolves.toBe(true);
});

No await, so the test finishes before the assertion runs and passes whatever save() does. I found 36 tests like this across 44 popular repos, and one project had 41 of them on its own. They all looked fine in review.

What I'm curious about is how teams actually stop this:

  • eslint-plugin-jest has valid-expect with alwaysAwait, and Vitest has its own rules. Do you turn these on, and do they catch every form (.rejects, helpers that wrap expect)?
  • @typescript-eslint/no-floating-promises catches it in TS, but only with type-aware linting. Is that worth the slower lint for you?
  • expect.assertions(n) / expect.hasAssertions() in every async test: does anyone enforce that, or is it too noisy?
  • Or is it just code review, and you accept that some slip through?

Which of these has actually worked on a real team, and which did you give up on?

10 Upvotes

18 comments sorted by

View all comments

1

u/p_n_48947 10d ago

Add a lint fixture for bare .resolves and a promise-returning helper, so rule changes exercise the wrapper case explicitly.