@@ -37,6 +37,8 @@ const { isPromise, isRegExp } = require('util/').types;
3737const objectAssign = Object . assign ? Object . assign : require ( 'es6-object-assign' ) . assign ;
3838const objectIs = Object . is ? Object . is : require ( 'object-is' ) ;
3939
40+ const RegExpPrototypeTest = require ( 'call-bind/callBound' ) ( 'RegExp.prototype.test' ) ;
41+
4042const errorCache = new Map ( ) ;
4143
4244let isDeepEqual ;
@@ -308,7 +310,7 @@ class Comparison {
308310 if ( actual !== undefined &&
309311 typeof actual [ key ] === 'string' &&
310312 isRegExp ( obj [ key ] ) &&
311- obj [ key ] . test ( actual [ key ] )
313+ RegExpPrototypeTest ( obj [ key ] , actual [ key ] )
312314 ) {
313315 this [ key ] = actual [ key ] ;
314316 } else {
@@ -350,7 +352,7 @@ function compareExceptionKey(actual, expected, key, message, keys, fn) {
350352function expectedException ( actual , expected , msg , fn ) {
351353 if ( typeof expected !== 'function' ) {
352354 if ( isRegExp ( expected ) )
353- return expected . test ( actual ) ;
355+ return RegExpPrototypeTest ( expected , actual ) ;
354356 // assert.doesNotThrow does not accept objects.
355357 if ( arguments . length === 2 ) {
356358 throw new ERR_INVALID_ARG_TYPE (
@@ -385,7 +387,7 @@ function expectedException(actual, expected, msg, fn) {
385387 if (
386388 typeof actual [ key ] === 'string' &&
387389 isRegExp ( expected [ key ] ) &&
388- expected [ key ] . test ( actual [ key ] )
390+ RegExpPrototypeTest ( expected [ key ] , actual [ key ] )
389391 ) {
390392 return ;
391393 }
@@ -596,6 +598,51 @@ assert.ifError = function ifError(err) {
596598 }
597599} ;
598600
601+ // Currently in sync with Node.js lib/assert.js
602+ // https://github.com/nodejs/node/commit/2a871df3dfb8ea663ef5e1f8f62701ec51384ecb
603+ function internalMatch ( string , regexp , message , fn , fnName ) {
604+ if ( ! isRegExp ( regexp ) ) {
605+ throw new ERR_INVALID_ARG_TYPE (
606+ 'regexp' , 'RegExp' , regexp
607+ ) ;
608+ }
609+ const match = fnName === 'match' ;
610+ if ( typeof string !== 'string' ||
611+ RegExpPrototypeTest ( regexp , string ) !== match ) {
612+ if ( message instanceof Error ) {
613+ throw message ;
614+ }
615+
616+ const generatedMessage = ! message ;
617+
618+ // 'The input was expected to not match the regular expression ' +
619+ message = message || ( typeof string !== 'string' ?
620+ 'The "string" argument must be of type string. Received type ' +
621+ `${ typeof string } (${ inspect ( string ) } )` :
622+ ( match ?
623+ 'The input did not match the regular expression ' :
624+ 'The input was expected to not match the regular expression ' ) +
625+ `${ inspect ( regexp ) } . Input:\n\n${ inspect ( string ) } \n` ) ;
626+ const err = new AssertionError ( {
627+ actual : string ,
628+ expected : regexp ,
629+ message,
630+ operator : fnName ,
631+ stackStartFn : fn
632+ } ) ;
633+ err . generatedMessage = generatedMessage ;
634+ throw err ;
635+ }
636+ }
637+
638+ assert . match = function match ( string , regexp , message ) {
639+ internalMatch ( string , regexp , message , match , 'match' ) ;
640+ } ;
641+
642+ assert . doesNotMatch = function doesNotMatch ( string , regexp , message ) {
643+ internalMatch ( string , regexp , message , doesNotMatch , 'doesNotMatch' ) ;
644+ } ;
645+
599646// Expose a strict only variant of assert
600647function strict ( ...args ) {
601648 innerOk ( strict , args . length , ...args ) ;
0 commit comments