@@ -718,17 +718,27 @@ function getActual(block) {
718718 }
719719}
720720
721- // Expected to throw an error.
722- assert . throws = function throws ( block , error , message ) {
723- const actual = getActual ( block ) ;
721+ async function waitForActual ( block ) {
722+ if ( typeof block !== 'function' ) {
723+ throw new errors . ERR_INVALID_ARG_TYPE ( 'block' , 'Function' , block ) ;
724+ }
725+ try {
726+ await block ( ) ;
727+ } catch ( e ) {
728+ return e ;
729+ }
730+ return errors . NO_EXCEPTION_SENTINEL ;
731+ }
724732
733+ // Expected to throw an error.
734+ function expectsError ( stackStartFn , actual , error , message ) {
725735 if ( typeof error === 'string' ) {
726- if ( arguments . length === 3 )
736+ if ( arguments . length === 4 ) {
727737 throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' ,
728738 'error' ,
729739 [ 'Function' , 'RegExp' ] ,
730740 error ) ;
731-
741+ }
732742 message = error ;
733743 error = null ;
734744 }
@@ -739,21 +749,21 @@ assert.throws = function throws(block, error, message) {
739749 details += ` (${ error . name } )` ;
740750 }
741751 details += message ? `: ${ message } ` : '.' ;
752+ const fnType = stackStartFn === rejects ? 'rejection' : 'exception' ;
742753 innerFail ( {
743754 actual,
744755 expected : error ,
745- operator : 'throws' ,
746- message : `Missing expected exception ${ details } ` ,
747- stackStartFn : throws
756+ operator : stackStartFn . name ,
757+ message : `Missing expected ${ fnType } ${ details } ` ,
758+ stackStartFn
748759 } ) ;
749760 }
750761 if ( error && expectedException ( actual , error , message ) === false ) {
751762 throw actual ;
752763 }
753- } ;
764+ }
754765
755- assert . doesNotThrow = function doesNotThrow ( block , error , message ) {
756- const actual = getActual ( block ) ;
766+ function expectsNoError ( stackStartFn , actual , error , message ) {
757767 if ( actual === undefined )
758768 return ;
759769
@@ -764,16 +774,41 @@ assert.doesNotThrow = function doesNotThrow(block, error, message) {
764774
765775 if ( ! error || expectedException ( actual , error ) ) {
766776 const details = message ? `: ${ message } ` : '.' ;
777+ const fnType = stackStartFn === doesNotReject ? 'rejection' : 'exception' ;
767778 innerFail ( {
768779 actual,
769780 expected : error ,
770- operator : 'doesNotThrow' ,
771- message : `Got unwanted exception ${ details } \n${ actual . message } ` ,
772- stackStartFn : doesNotThrow
781+ operator : stackStartFn . name ,
782+ message : `Got unwanted ${ fnType } ${ details } \n${ actual . message } ` ,
783+ stackStartFn
773784 } ) ;
774785 }
775786 throw actual ;
776- } ;
787+ }
788+
789+ function throws ( block , ...args ) {
790+ expectsError ( throws , getActual ( block ) , ...args ) ;
791+ }
792+
793+ assert . throws = throws ;
794+
795+ async function rejects ( block , ...args ) {
796+ expectsError ( rejects , await waitForActual ( block ) , ...args ) ;
797+ }
798+
799+ assert . rejects = rejects ;
800+
801+ function doesNotThrow ( block , ...args ) {
802+ expectsNoError ( doesNotThrow , getActual ( block ) , ...args ) ;
803+ }
804+
805+ assert . doesNotThrow = doesNotThrow ;
806+
807+ async function doesNotReject ( block , ...args ) {
808+ expectsNoError ( doesNotReject , await waitForActual ( block ) , ...args ) ;
809+ }
810+
811+ assert . doesNotReject = doesNotReject ;
777812
778813assert . ifError = function ifError ( err ) { if ( err ) throw err ; } ;
779814
0 commit comments