prefer-assert-match
July 23, 2026 ยท View on GitHub
๐ Prefer assert.match()/assert.doesNotMatch() over asserting RegExp#test() / String#match() results.
๐ผ This rule is enabled in the following configs: โ
recommended, โ๏ธ unopinionated.
๐ง This rule is automatically fixable by the --fix CLI option.
Prefer dedicated assertion methods over asserting the boolean result of regex methods. This makes test failures more informative and communicates the intent more clearly.
String#match() returns Array | null, not a boolean, so only its truthiness forms (like assert.ok(str.match(/re/))) are matched. Comparing the result to a boolean literal is a test bug rather than a style issue, and rewriting it would change the outcome, so the rule leaves it alone.
Examples
import assert from 'node:assert';
// โ
assert.ok(/^foo/.test(str));
assert.ok(str.match(/^foo/));
assert.strictEqual(/^foo/.test(str), true);
// โ
assert.match(str, /^foo/);
import assert from 'node:assert';
// โ
assert.ok(!/^foo/.test(str));
assert.strictEqual(/^foo/.test(str), false);
assert.notStrictEqual(/^foo/.test(str), true);
// โ
assert.doesNotMatch(str, /^foo/);