@@ -496,6 +496,195 @@ describe('expect()', () => {
496
496
} ) ;
497
497
} ) ;
498
498
499
+ describe ( 'error()' , ( ) => {
500
+
501
+ const error = new Error ( 'kaboom' ) ;
502
+
503
+ it ( 'validates assertion' , ( done ) => {
504
+
505
+ let exception = false ;
506
+ try {
507
+ Code . expect ( error ) . to . be . an . error ( ) ;
508
+ }
509
+ catch ( err ) {
510
+ exception = err ;
511
+ }
512
+
513
+ Hoek . assert ( ! exception , exception ) ;
514
+ done ( ) ;
515
+ } ) ;
516
+
517
+ it ( 'validates assertion (not error)' , ( done ) => {
518
+
519
+ const Custom = function ( ) { } ;
520
+ Hoek . inherits ( Custom , Error ) ;
521
+
522
+ let exception = false ;
523
+ try {
524
+ Code . expect ( false ) . to . not . be . an . error ( ) ;
525
+ Code . expect ( new Error ( 'kaboom' ) ) . to . not . be . an . error ( 'baboom' ) ;
526
+ Code . expect ( new Error ( 'kaboom' ) ) . to . not . be . an . error ( Error , 'baboom' ) ;
527
+ Code . expect ( new Error ( ) ) . to . not . be . an . error ( Custom ) ;
528
+ Code . expect ( new Error ( 'kaboom' ) ) . to . not . be . an . error ( Custom , 'baboom' ) ;
529
+ }
530
+ catch ( err ) {
531
+ exception = err ;
532
+ }
533
+
534
+ Hoek . assert ( ! exception , exception ) ;
535
+ done ( ) ;
536
+ } ) ;
537
+
538
+ it ( 'invalidates assertion' , ( done ) => {
539
+
540
+ let exception = false ;
541
+ try {
542
+ Code . expect ( false ) . to . be . an . error ( ) ;
543
+ }
544
+ catch ( err ) {
545
+ exception = err ;
546
+ }
547
+
548
+ Hoek . assert ( exception . message === 'Expected false to be an error with Error type' , exception ) ;
549
+ done ( ) ;
550
+ } ) ;
551
+
552
+ it ( 'validates assertion (message)' , ( done ) => {
553
+
554
+ let exception = false ;
555
+ try {
556
+ Code . expect ( error ) . to . be . an . error ( 'kaboom' ) ;
557
+ }
558
+ catch ( err ) {
559
+ exception = err ;
560
+ }
561
+
562
+ Hoek . assert ( ! exception , exception ) ;
563
+ done ( ) ;
564
+ } ) ;
565
+
566
+ it ( 'validates assertion (empty message)' , ( done ) => {
567
+
568
+ let exception = false ;
569
+ try {
570
+ Code . expect ( new Error ( '' ) ) . to . be . an . error ( '' ) ;
571
+ }
572
+ catch ( err ) {
573
+ exception = err ;
574
+ }
575
+
576
+ Hoek . assert ( ! exception , exception ) ;
577
+ done ( ) ;
578
+ } ) ;
579
+
580
+ it ( 'validates assertion (message regex)' , ( done ) => {
581
+
582
+ let exception = false ;
583
+ try {
584
+ Code . expect ( error ) . to . be . an . error ( / b o o m / ) ;
585
+ }
586
+ catch ( err ) {
587
+ exception = err ;
588
+ }
589
+
590
+ Hoek . assert ( ! exception , exception ) ;
591
+ done ( ) ;
592
+ } ) ;
593
+
594
+ it ( 'validates assertion (missing message)' , ( done ) => {
595
+
596
+ const Custom = function ( ) { } ;
597
+ Hoek . inherits ( Custom , Error ) ;
598
+
599
+ let exception = false ;
600
+ try {
601
+ Code . expect ( new Custom ( ) ) . to . be . an . error ( 'kaboom' ) ;
602
+ }
603
+ catch ( err ) {
604
+ exception = err ;
605
+ }
606
+
607
+ Hoek . assert ( exception . message === 'Expected [Error] to be an error with specified message' , exception ) ;
608
+ done ( ) ;
609
+ } ) ;
610
+
611
+
612
+ it ( 'invalidates assertion (empty message)' , ( done ) => {
613
+
614
+ let exception = false ;
615
+ try {
616
+ Code . expect ( new Error ( 'kaboom' ) ) . to . be . an . error ( '' ) ;
617
+ }
618
+ catch ( err ) {
619
+ exception = err ;
620
+ }
621
+
622
+ Hoek . assert ( exception . message === 'Expected [Error: kaboom] to be an error with specified message' , exception ) ;
623
+ done ( ) ;
624
+ } ) ;
625
+
626
+ it ( 'validates assertion (type)' , ( done ) => {
627
+
628
+ let exception = false ;
629
+ try {
630
+ Code . expect ( error ) . to . be . an . error ( Error ) ;
631
+ }
632
+ catch ( err ) {
633
+ exception = err ;
634
+ }
635
+
636
+ Hoek . assert ( ! exception , exception ) ;
637
+ done ( ) ;
638
+ } ) ;
639
+
640
+ it ( 'invalidates assertion (known type)' , ( done ) => {
641
+
642
+ const Custom = function ( ) { } ;
643
+
644
+ let exception = false ;
645
+ try {
646
+ Code . expect ( new Custom ( ) ) . to . be . an . error ( Error ) ;
647
+ }
648
+ catch ( err ) {
649
+ exception = err ;
650
+ }
651
+
652
+ Hoek . assert ( exception . message === 'Expected {} to be an error with Error type' , exception ) ;
653
+ done ( ) ;
654
+ } ) ;
655
+
656
+ it ( 'invalidates assertion (anonymous type)' , ( done ) => {
657
+
658
+ const Custom = function ( ) { } ;
659
+ Hoek . inherits ( Custom , Error ) ;
660
+
661
+ let exception = false ;
662
+ try {
663
+ Code . expect ( error ) . to . be . an . error ( Custom ) ;
664
+ }
665
+ catch ( err ) {
666
+ exception = err ;
667
+ }
668
+
669
+ Hoek . assert ( exception . message === 'Expected [Error: kaboom] to be an error with provided type' , exception ) ;
670
+ done ( ) ;
671
+ } ) ;
672
+
673
+ it ( 'validates assertion (type and message)' , ( done ) => {
674
+
675
+ let exception = false ;
676
+ try {
677
+ Code . expect ( error ) . to . be . an . error ( Error , 'kaboom' ) ;
678
+ }
679
+ catch ( err ) {
680
+ exception = err ;
681
+ }
682
+
683
+ Hoek . assert ( ! exception , exception ) ;
684
+ done ( ) ;
685
+ } ) ;
686
+ } ) ;
687
+
499
688
describe ( 'function()' , ( ) => {
500
689
501
690
it ( 'validates correct type' , ( done ) => {
0 commit comments