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);