require-throws-expectation
July 2, 2026 ยท View on GitHub
๐ Require an error matcher for assert.throws()/assert.rejects().
๐ผ This rule is enabled in the following configs: โ
recommended, โ๏ธ unopinionated.
assert.throws(fn) and assert.rejects(asyncFn) with no second argument pass for any thrown value. That makes the assertion weak: a typo, a ReferenceError, or an unrelated failure all satisfy it, so the test can pass for the wrong reason. Pass an error matcher โ an error class, a RegExp for the message, a validation object, or a validation function โ to assert that the expected error is thrown.
This rule reports a single-argument assert.throws()/assert.rejects(). A string second argument is reported by no-assert-throws-string instead.
Examples
import assert from 'node:assert';
// โ
assert.throws(() => parse(input));
await assert.rejects(() => load(url));
// โ
assert.throws(() => parse(input), SyntaxError);
await assert.rejects(() => load(url), {code: 'ENOENT'});