@@ -25,6 +25,7 @@ const { isSet, isMap, isDate, isRegExp } = process.binding('util');
25
25
const { objectToString } = require ( 'internal/util' ) ;
26
26
const { isArrayBufferView } = require ( 'internal/util/types' ) ;
27
27
const errors = require ( 'internal/errors' ) ;
28
+ const { inspect } = require ( 'util' ) ;
28
29
29
30
// The assert module provides functions that throw
30
31
// AssertionError's when particular conditions are not met. The
@@ -660,10 +661,44 @@ assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
660
661
}
661
662
} ;
662
663
663
- function expectedException ( actual , expected ) {
664
+ function createMsg ( msg , key , actual , expected ) {
665
+ if ( msg )
666
+ return msg ;
667
+ return `${ key } : expected ${ inspect ( expected [ key ] ) } , ` +
668
+ `not ${ inspect ( actual [ key ] ) } ` ;
669
+ }
670
+
671
+ function expectedException ( actual , expected , msg ) {
664
672
if ( typeof expected !== 'function' ) {
665
- // Should be a RegExp, if not fail hard
666
- return expected . test ( actual ) ;
673
+ if ( expected instanceof RegExp )
674
+ return expected . test ( actual ) ;
675
+ // assert.doesNotThrow does not accept objects.
676
+ if ( arguments . length === 2 ) {
677
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'expected' ,
678
+ [ 'Function' , 'RegExp' ] , expected ) ;
679
+ }
680
+ // The name and message could be non enumerable. Therefore test them
681
+ // explicitly.
682
+ if ( 'name' in expected ) {
683
+ assert . strictEqual (
684
+ actual . name ,
685
+ expected . name ,
686
+ createMsg ( msg , 'name' , actual , expected ) ) ;
687
+ }
688
+ if ( 'message' in expected ) {
689
+ assert . strictEqual (
690
+ actual . message ,
691
+ expected . message ,
692
+ createMsg ( msg , 'message' , actual , expected ) ) ;
693
+ }
694
+ const keys = Object . keys ( expected ) ;
695
+ for ( const key of keys ) {
696
+ assert . deepStrictEqual (
697
+ actual [ key ] ,
698
+ expected [ key ] ,
699
+ createMsg ( msg , key , actual , expected ) ) ;
700
+ }
701
+ return true ;
667
702
}
668
703
// Guard instanceof against arrow functions as they don't have a prototype.
669
704
if ( expected . prototype !== undefined && actual instanceof expected ) {
@@ -716,7 +751,7 @@ assert.throws = function throws(block, error, message) {
716
751
stackStartFn : throws
717
752
} ) ;
718
753
}
719
- if ( error && expectedException ( actual , error ) === false ) {
754
+ if ( error && expectedException ( actual , error , message ) === false ) {
720
755
throw actual ;
721
756
}
722
757
} ;
0 commit comments