File tree 2 files changed +65
-0
lines changed
2 files changed +65
-0
lines changed Original file line number Diff line number Diff line change @@ -420,6 +420,10 @@ internals.reject = async function (/* type, message */) {
420
420
internals . assert ( this , ! this . _flags . not || ! arguments . length , 'Cannot specify arguments when expecting not to reject' ) ;
421
421
422
422
if ( thrown ) {
423
+
424
+ internals . assert ( this , arguments . length < 2 || message , 'Can not assert with invalid message argument type' ) ;
425
+ internals . assert ( this , arguments . length < 1 || message !== null || typeof type === 'function' , 'Can not assert with invalid type argument' ) ;
426
+
423
427
if ( type ) {
424
428
this . assert ( thrown instanceof type , 'reject with ' + ( type . name || 'provided type' ) ) ;
425
429
}
Original file line number Diff line number Diff line change @@ -2436,6 +2436,67 @@ describe('expect()', () => {
2436
2436
Hoek . assert ( / E x p e c t e d \[ P r o m i s e \] t o r e j e c t w i t h p r o v i d e d t y p e / . test ( exception . message ) , exception ) ;
2437
2437
} ) ;
2438
2438
2439
+ it ( 'invalidates rejection (invalid type)' , async ( ) => {
2440
+
2441
+ const promise = Promise . reject ( new Error ( 'kaboom' ) ) ;
2442
+
2443
+ const fail = async ( value ) => {
2444
+
2445
+ let exception = false ;
2446
+
2447
+ try {
2448
+ await Code . expect ( promise ) . to . reject ( value ) ;
2449
+ }
2450
+ catch ( err ) {
2451
+ exception = err ;
2452
+ }
2453
+
2454
+ Hoek . assert ( exception . message === 'Can not assert with invalid type argument' , exception ) ;
2455
+ } ;
2456
+
2457
+ await fail ( 0 ) ;
2458
+ await fail ( 1 ) ;
2459
+ await fail ( Infinity ) ;
2460
+ await fail ( undefined ) ;
2461
+ await fail ( null ) ;
2462
+ await fail ( true ) ;
2463
+ await fail ( false ) ;
2464
+ await fail ( { } ) ;
2465
+ await fail ( [ ] ) ;
2466
+ await fail ( NaN ) ;
2467
+ } ) ;
2468
+
2469
+ it ( 'invalidates rejection (invalid message type)' , async ( ) => {
2470
+
2471
+ const promise = Promise . reject ( new Error ( 'kaboom' ) ) ;
2472
+
2473
+ const fail = async ( value ) => {
2474
+
2475
+ let exception = false ;
2476
+
2477
+ try {
2478
+
2479
+ await Code . expect ( promise ) . to . reject ( Error , value ) ;
2480
+ }
2481
+ catch ( err ) {
2482
+ exception = err ;
2483
+ }
2484
+
2485
+ Hoek . assert ( exception . message === 'Can not assert with invalid message argument type' , exception ) ;
2486
+ } ;
2487
+
2488
+ await fail ( 1 ) ;
2489
+ await fail ( 0 ) ;
2490
+ await fail ( Infinity ) ;
2491
+ await fail ( undefined ) ;
2492
+ await fail ( null ) ;
2493
+ await fail ( true ) ;
2494
+ await fail ( false ) ;
2495
+ await fail ( { } ) ;
2496
+ await fail ( [ ] ) ;
2497
+ await fail ( NaN ) ;
2498
+ } ) ;
2499
+
2439
2500
it ( 'validates rejection (type and message)' , async ( ) => {
2440
2501
2441
2502
let exception = false ;
You can’t perform that action at this time.
0 commit comments