@@ -675,68 +675,73 @@ function expectedException(actual, expected) {
675
675
return expected . call ( { } , actual ) === true ;
676
676
}
677
677
678
- function tryBlock ( block ) {
678
+ function getActual ( block ) {
679
+ if ( typeof block !== 'function' ) {
680
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'block' , 'Function' ,
681
+ block ) ;
682
+ }
679
683
try {
680
684
block ( ) ;
681
685
} catch ( e ) {
682
686
return e ;
683
687
}
684
688
}
685
689
686
- function innerThrows ( shouldThrow , block , expected , message ) {
687
- var details = '' ;
690
+ // Expected to throw an error.
691
+ assert . throws = function throws ( block , error , message ) {
692
+ const actual = getActual ( block ) ;
688
693
689
- if ( typeof block !== 'function' ) {
690
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'block' , 'function' ,
691
- block ) ;
692
- }
694
+ if ( typeof error === 'string' ) {
695
+ if ( arguments . length === 3 )
696
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' ,
697
+ 'error' ,
698
+ [ 'Function' , 'RegExp' ] ,
699
+ error ) ;
693
700
694
- if ( typeof expected === 'string' ) {
695
- message = expected ;
696
- expected = null ;
701
+ message = error ;
702
+ error = null ;
697
703
}
698
704
699
- const actual = tryBlock ( block ) ;
700
-
701
- if ( shouldThrow === true ) {
702
- if ( actual === undefined ) {
703
- if ( expected && expected . name ) {
704
- details += ` (${ expected . name } )` ;
705
- }
706
- details += message ? `: ${ message } ` : '.' ;
707
- innerFail ( {
708
- actual,
709
- expected,
710
- operator : 'throws' ,
711
- message : `Missing expected exception${ details } ` ,
712
- stackStartFn : assert . throws
713
- } ) ;
714
- }
715
- if ( expected && expectedException ( actual , expected ) === false ) {
716
- throw actual ;
717
- }
718
- } else if ( actual !== undefined ) {
719
- if ( ! expected || expectedException ( actual , expected ) ) {
720
- details = message ? `: ${ message } ` : '.' ;
721
- innerFail ( {
722
- actual,
723
- expected,
724
- operator : 'doesNotThrow' ,
725
- message : `Got unwanted exception${ details } ` ,
726
- stackStartFn : assert . doesNotThrow
727
- } ) ;
705
+ if ( actual === undefined ) {
706
+ let details = '' ;
707
+ if ( error && error . name ) {
708
+ details += ` (${ error . name } )` ;
728
709
}
710
+ details += message ? `: ${ message } ` : '.' ;
711
+ innerFail ( {
712
+ actual,
713
+ expected : error ,
714
+ operator : 'throws' ,
715
+ message : `Missing expected exception${ details } ` ,
716
+ stackStartFn : throws
717
+ } ) ;
718
+ }
719
+ if ( error && expectedException ( actual , error ) === false ) {
729
720
throw actual ;
730
721
}
731
- }
732
-
733
- // Expected to throw an error.
734
- assert . throws = function throws ( block , error , message ) {
735
- innerThrows ( true , block , error , message ) ;
736
722
} ;
737
723
738
724
assert . doesNotThrow = function doesNotThrow ( block , error , message ) {
739
- innerThrows ( false , block , error , message ) ;
725
+ const actual = getActual ( block ) ;
726
+ if ( actual === undefined )
727
+ return ;
728
+
729
+ if ( typeof error === 'string' ) {
730
+ message = error ;
731
+ error = null ;
732
+ }
733
+
734
+ if ( ! error || expectedException ( actual , error ) ) {
735
+ const details = message ? `: ${ message } ` : '.' ;
736
+ innerFail ( {
737
+ actual,
738
+ expected : error ,
739
+ operator : 'doesNotThrow' ,
740
+ message : `Got unwanted exception${ details } \n${ actual . message } ` ,
741
+ stackStartFn : doesNotThrow
742
+ } ) ;
743
+ }
744
+ throw actual ;
740
745
} ;
741
746
742
747
assert . ifError = function ifError ( err ) { if ( err ) throw err ; } ;
0 commit comments