@@ -37,6 +37,8 @@ const { isPromise, isRegExp } = require('util/').types;
37
37
const objectAssign = Object . assign ? Object . assign : require ( 'es6-object-assign' ) . assign ;
38
38
const objectIs = Object . is ? Object . is : require ( 'object-is' ) ;
39
39
40
+ const RegExpPrototypeTest = require ( 'call-bind/callBound' ) ( 'RegExp.prototype.test' ) ;
41
+
40
42
const errorCache = new Map ( ) ;
41
43
42
44
let isDeepEqual ;
@@ -308,7 +310,7 @@ class Comparison {
308
310
if ( actual !== undefined &&
309
311
typeof actual [ key ] === 'string' &&
310
312
isRegExp ( obj [ key ] ) &&
311
- obj [ key ] . test ( actual [ key ] )
313
+ RegExpPrototypeTest ( obj [ key ] , actual [ key ] )
312
314
) {
313
315
this [ key ] = actual [ key ] ;
314
316
} else {
@@ -350,7 +352,7 @@ function compareExceptionKey(actual, expected, key, message, keys, fn) {
350
352
function expectedException ( actual , expected , msg , fn ) {
351
353
if ( typeof expected !== 'function' ) {
352
354
if ( isRegExp ( expected ) )
353
- return expected . test ( actual ) ;
355
+ return RegExpPrototypeTest ( expected , actual ) ;
354
356
// assert.doesNotThrow does not accept objects.
355
357
if ( arguments . length === 2 ) {
356
358
throw new ERR_INVALID_ARG_TYPE (
@@ -385,7 +387,7 @@ function expectedException(actual, expected, msg, fn) {
385
387
if (
386
388
typeof actual [ key ] === 'string' &&
387
389
isRegExp ( expected [ key ] ) &&
388
- expected [ key ] . test ( actual [ key ] )
390
+ RegExpPrototypeTest ( expected [ key ] , actual [ key ] )
389
391
) {
390
392
return ;
391
393
}
@@ -596,6 +598,51 @@ assert.ifError = function ifError(err) {
596
598
}
597
599
} ;
598
600
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
+
599
646
// Expose a strict only variant of assert
600
647
function strict ( ...args ) {
601
648
innerOk ( strict , args . length , ...args ) ;
0 commit comments