no-assert-throws-string

July 2, 2026 ยท View on GitHub

๐Ÿ“ Disallow a string as the error matcher of assert.throws()/assert.rejects().

๐Ÿ’ผ This rule is enabled in the following configs: โœ… recommended, โ˜‘๏ธ unopinionated.

๐Ÿ’ก This rule is manually fixable by editor suggestions.

assert.throws(fn[, error][, message]) and assert.rejects(asyncFn[, error][, message]) take an optional error matcher followed by an optional failure message. A string in the matcher position is interpreted as the message, not as something to validate the thrown error against. So assert.throws(fn, 'Wrong value') passes for any thrown error, and if the thrown error's message happens to equal the string, Node throws ERR_AMBIGUOUS_ARGUMENT. From the Node.js docs: "if a string is provided as the second argument, then error is assumed to be omitted... This can lead to easy-to-miss mistakes."

This rule reports a string (or template literal) passed as the second argument to assert.throws()/assert.rejects(). The suggestion rewrites it to a validation object that matches the error message.

Examples

import assert from 'node:assert';

// โŒ
assert.throws(fn, 'Wrong value');
assert.rejects(asyncFn, 'Wrong value');

// โœ…
assert.throws(fn, {message: 'Wrong value'});
assert.throws(fn, /Wrong value/);
assert.throws(fn, TypeError);

// โœ… (a string in the third position is the failure message)
assert.throws(fn, TypeError, 'should have thrown a TypeError');