toThrow is given an async function, whose errors become a rejected promise instead of a throw

tx0/ts:async-callback-in-throw-assertiondefaultwarningmaintainability/testsdepth fastbug

What it finds

An async function never throws when called; its errors reject the promise it returns. expect(async () => ...).toThrow() therefore sees no exception and fails, or passes when negated, whatever the code does. await expect(...).rejects.toThrow() checks the rejection.

Reported are expect of an async arrow function or function expression followed by toThrow or toThrowError without rejects.

From the rule's source, async_callback_in_throw_assertion.rs.

Example from the rule's tests
it("fails", async () => {  expect(async () => fetchUser()).toThrow();tx0 reports: an async function rejects instead of throwing  await expect(fetchUser()).rejects.toThrow();});declare function fetchUser(): Promise<void>;
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/ts:async-callback-in-throw-assertion" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/ts:async-callback-in-throw-assertion] reason="..."

tx0 explain tx0/ts:async-callback-in-throw-assertion shows the same in the terminal. Found a false positive? Report it.