diff --git a/lib/assert.js b/lib/assert.js index 6ad672d698602d..6a3d1198343d63 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -886,7 +886,7 @@ assert.ifError = function ifError(err) { } }; -function internalMatch(string, regexp, message, fn) { +function internalMatch(string, regexp, message, fn, shouldThrow = true) { if (!isRegExp(regexp)) { throw new ERR_INVALID_ARG_TYPE( 'regexp', 'RegExp', regexp @@ -896,9 +896,11 @@ function internalMatch(string, regexp, message, fn) { if (typeof string !== 'string' || RegExpPrototypeTest(regexp, string) !== match) { if (message instanceof Error) { + if (!shouldThrow) return false; throw message; } + if (!shouldThrow) return false; const generatedMessage = !message; // 'The input was expected to not match the regular expression ' + @@ -919,10 +921,14 @@ function internalMatch(string, regexp, message, fn) { err.generatedMessage = generatedMessage; throw err; } + if (!shouldThrow) return true; } -assert.match = function match(string, regexp, message) { - internalMatch(string, regexp, message, match); +assert.match = function match(string, regexp, message, shouldThrow) { + // Undefined shouldThrow and message as boolean + // means message is shouldThrow. + if (!shouldThrow && typeof message === 'boolean') shouldThrow = message; + return internalMatch(string, regexp, message, match, shouldThrow); }; assert.doesNotMatch = function doesNotMatch(string, regexp, message) { diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index cbbf091f04b8a7..dc11dd07a92ca9 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -1418,6 +1418,12 @@ assert.throws( assert.match('I will pass', /pass$/); } +// Use the assert.match boolean result. +{ + assert(assert.match('meow', /meow$/, false)); + assert(!assert.match('meow', /woof$/, false)); +} + // Multiple assert.doesNotMatch() tests. { assert.throws(