@@ -899,6 +899,78 @@ test('.throws()', gather(t => {
899899 formatted : / n u l l / ,
900900 } ] ,
901901 } ) ;
902+
903+ // Fails because the string in the message is incorrect
904+ failsWith (
905+ t ,
906+ ( ) =>
907+ assertions . throws (
908+ ( ) => {
909+ throw new Error ( 'error' ) ;
910+ } ,
911+ { message : 'my error' } ,
912+ ) ,
913+ {
914+ assertion : 'throws' ,
915+ message : '' ,
916+ values : [
917+ { label : 'Function threw unexpected exception:' , formatted : / e r r o r / } ,
918+ { label : 'Expected message to equal:' , formatted : / m y e r r o r / } ,
919+ ] ,
920+ } ,
921+ ) ;
922+
923+ passes ( t , ( ) => assertions . throws ( ( ) => {
924+ throw new Error ( 'error' ) ;
925+ } , { message : 'error' } ) ) ;
926+
927+ // Fails because the regular expression in the message is incorrect
928+ failsWith (
929+ t ,
930+ ( ) =>
931+ assertions . throws (
932+ ( ) => {
933+ throw new Error ( 'error' ) ;
934+ } ,
935+ { message : / m y e r r o r / } ,
936+ ) ,
937+ {
938+ assertion : 'throws' ,
939+ message : '' ,
940+ values : [
941+ { label : 'Function threw unexpected exception:' , formatted : / e r r o r / } ,
942+ { label : 'Expected message to match:' , formatted : / m y e r r o r / } ,
943+ ] ,
944+ } ,
945+ ) ;
946+
947+ passes ( t , ( ) => assertions . throws ( ( ) => {
948+ throw new Error ( 'error' ) ;
949+ } , { message : / e r r o r / } ) ) ;
950+
951+ // Fails because the function in the message returns false
952+ failsWith (
953+ t ,
954+ ( ) =>
955+ assertions . throws (
956+ ( ) => {
957+ throw new Error ( 'error' ) ;
958+ } ,
959+ { message : ( ) => false } ,
960+ ) ,
961+ {
962+ assertion : 'throws' ,
963+ message : '' ,
964+ values : [
965+ { label : 'Function threw unexpected exception:' , formatted : / e r r o r / } ,
966+ { label : 'Expected message to return true:' , formatted : / F u n c t i o n / } ,
967+ ] ,
968+ } ,
969+ ) ;
970+
971+ passes ( t , ( ) => assertions . throws ( ( ) => {
972+ throw new Error ( 'error' ) ;
973+ } , { message : ( ) => true } ) ) ;
902974} ) ) ;
903975
904976test ( '.throws() returns the thrown error' , t => {
@@ -1066,7 +1138,7 @@ test('.throws() fails if passed a bad expectation', t => {
10661138
10671139 failsWith ( t , ( ) => assertions . throws ( ( ) => { } , { message : null } ) , {
10681140 assertion : 'throws' ,
1069- message : 'The `message` property of the second argument to `t.throws()` must be a string or regular expression' ,
1141+ message : 'The `message` property of the second argument to `t.throws()` must be a string, regular expression or a function ' ,
10701142 values : [ { label : 'Called with:' , formatted : / m e s s a g e : n u l l / } ] ,
10711143 } ) ;
10721144
@@ -1136,7 +1208,7 @@ test('.throwsAsync() fails if passed a bad expectation', t => {
11361208
11371209 failsWith ( t , ( ) => assertions . throwsAsync ( ( ) => { } , { message : null } ) , {
11381210 assertion : 'throwsAsync' ,
1139- message : 'The `message` property of the second argument to `t.throwsAsync()` must be a string or regular expression' ,
1211+ message : 'The `message` property of the second argument to `t.throwsAsync()` must be a string, regular expression or a function ' ,
11401212 values : [ { label : 'Called with:' , formatted : / m e s s a g e : n u l l / } ] ,
11411213 } , { expectBoolean : false } ) ;
11421214
0 commit comments