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