Skip to main content

require-error-message-assertions

Require throw and rejection assertions to verify the expected error.

Rule detailsโ€‹

toThrow() without an expected message, constructor, pattern, or shape only proves that some exception occurred. That leaves negative-path tests vulnerable to passing when the code throws for the wrong reason, such as an unrelated validation failure or a regression in setup.

Incorrectโ€‹

it("rejects invalid tokens", () => {
expect(() => parseToken("")).toThrow();
});
it("rejects missing users", async () => {
await expect(loadUser("missing")).rejects.toThrow();
});

Correctโ€‹

it("rejects invalid tokens", () => {
expect(() => parseToken("")).toThrow("Token is required");
});
it("rejects missing users", async () => {
await expect(loadUser("missing")).rejects.toThrow("Missing user");
});

What this rule reportsโ€‹

This rule reports executable it(...) and test(...) callbacks that call toThrow() or toThrowError() with no expected error argument. It does not report not.toThrow() because that assertion verifies that no error is thrown.

Skipped and todo tests are left to no-disabled-tests.

Optionsโ€‹

This rule has no options.

When not to use itโ€‹

Disable this rule for smoke tests where the exact error is intentionally irrelevant. Public behavior tests should usually assert the error message, constructor, pattern, or object shape.

Rule catalog ID: R012