prefer-assert-throws
July 2, 2026 Β· View on GitHub
π Prefer assert.throws()/assert.rejects() over try/catch with an assertion.
πΌπ« This rule is enabled in the β
recommended config. This rule is disabled in the βοΈ unopinionated config.
Using assert.throws() or assert.rejects() is cleaner and more expressive than wrapping throwing code in a try/catch with assertions in the catch block.
Examples
import assert from 'node:assert';
// β
try {
throwingFn();
} catch (error) {
assert.ok(error instanceof TypeError);
}
// β
try {
await asyncFn();
} catch (error) {
assert.ok(error instanceof TypeError);
}
// β
assert.throws(() => throwingFn(), TypeError);
// β
await assert.rejects(() => asyncFn(), TypeError);