@@ -25,6 +25,7 @@ const { isSet, isMap, isDate, isRegExp } = process.binding('util');
2525const { objectToString } = require ( 'internal/util' ) ;
2626const { isArrayBufferView } = require ( 'internal/util/types' ) ;
2727const errors = require ( 'internal/errors' ) ;
28+ const { inspect } = require ( 'util' ) ;
2829
2930// The assert module provides functions that throw
3031// AssertionError's when particular conditions are not met. The
@@ -660,10 +661,44 @@ assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
660661 }
661662} ;
662663
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 ) {
664672 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 ;
667702 }
668703 // Guard instanceof against arrow functions as they don't have a prototype.
669704 if ( expected . prototype !== undefined && actual instanceof expected ) {
@@ -716,7 +751,7 @@ assert.throws = function throws(block, error, message) {
716751 stackStartFn : throws
717752 } ) ;
718753 }
719- if ( error && expectedException ( actual , error ) === false ) {
754+ if ( error && expectedException ( actual , error , message ) === false ) {
720755 throw actual ;
721756 }
722757} ;
0 commit comments