@@ -593,13 +593,14 @@ function build (opts = {}) {
593
593
const key = Object . keys ( action . match ) [ 0 ]
594
594
match (
595
595
// in some cases, the yaml refers to the body with an empty string
596
- key === '$body' || key === ''
596
+ key . split ( '.' ) [ 0 ] === '$body' || key === ''
597
597
? response
598
598
: delve ( response , fillStashedValues ( key ) ) ,
599
- key === '$body'
599
+ key . split ( '.' ) [ 0 ] === '$body'
600
600
? action . match [ key ]
601
601
: fillStashedValues ( action . match ) [ key ] ,
602
- action . match
602
+ action . match ,
603
+ response
603
604
)
604
605
}
605
606
@@ -608,7 +609,8 @@ function build (opts = {}) {
608
609
const key = Object . keys ( action . lt ) [ 0 ]
609
610
lt (
610
611
delve ( response , fillStashedValues ( key ) ) ,
611
- fillStashedValues ( action . lt ) [ key ]
612
+ fillStashedValues ( action . lt ) [ key ] ,
613
+ response
612
614
)
613
615
}
614
616
@@ -617,7 +619,8 @@ function build (opts = {}) {
617
619
const key = Object . keys ( action . gt ) [ 0 ]
618
620
gt (
619
621
delve ( response , fillStashedValues ( key ) ) ,
620
- fillStashedValues ( action . gt ) [ key ]
622
+ fillStashedValues ( action . gt ) [ key ] ,
623
+ response
621
624
)
622
625
}
623
626
@@ -626,7 +629,8 @@ function build (opts = {}) {
626
629
const key = Object . keys ( action . lte ) [ 0 ]
627
630
lte (
628
631
delve ( response , fillStashedValues ( key ) ) ,
629
- fillStashedValues ( action . lte ) [ key ]
632
+ fillStashedValues ( action . lte ) [ key ] ,
633
+ response
630
634
)
631
635
}
632
636
@@ -635,7 +639,8 @@ function build (opts = {}) {
635
639
const key = Object . keys ( action . gte ) [ 0 ]
636
640
gte (
637
641
delve ( response , fillStashedValues ( key ) ) ,
638
- fillStashedValues ( action . gte ) [ key ]
642
+ fillStashedValues ( action . gte ) [ key ] ,
643
+ response
639
644
)
640
645
}
641
646
@@ -648,7 +653,8 @@ function build (opts = {}) {
648
653
: delve ( response , fillStashedValues ( key ) ) ,
649
654
key === '$body'
650
655
? action . length [ key ]
651
- : fillStashedValues ( action . length ) [ key ]
656
+ : fillStashedValues ( action . length ) [ key ] ,
657
+ response
652
658
)
653
659
}
654
660
@@ -657,7 +663,8 @@ function build (opts = {}) {
657
663
const isTrue = fillStashedValues ( action . is_true )
658
664
is_true (
659
665
delve ( response , isTrue ) ,
660
- isTrue
666
+ isTrue ,
667
+ response
661
668
)
662
669
}
663
670
@@ -666,7 +673,8 @@ function build (opts = {}) {
666
673
const isFalse = fillStashedValues ( action . is_false )
667
674
is_false (
668
675
delve ( response , isFalse ) ,
669
- isFalse
676
+ isFalse ,
677
+ response
670
678
)
671
679
}
672
680
}
@@ -679,46 +687,67 @@ function build (opts = {}) {
679
687
* Asserts that the given value is truthy
680
688
* @param {any } the value to check
681
689
* @param {string } an optional message
690
+ * @param {any } debugging metadata to attach to any assertion errors
682
691
* @returns {TestRunner }
683
692
*/
684
- function is_true ( val , msg ) {
685
- assert . ok ( val , `expect truthy value: ${ msg } - value: ${ JSON . stringify ( val ) } ` )
693
+ function is_true ( val , msg , response ) {
694
+ try {
695
+ assert . ok ( ( typeof val === 'string' && val . toLowerCase ( ) === 'true' ) || val , `expect truthy value: ${ msg } - value: ${ JSON . stringify ( val ) } ` )
696
+ } catch ( err ) {
697
+ err . response = JSON . stringify ( response )
698
+ throw err
699
+ }
686
700
}
687
701
688
702
/**
689
703
* Asserts that the given value is falsey
690
704
* @param {any } the value to check
691
705
* @param {string } an optional message
706
+ * @param {any } debugging metadata to attach to any assertion errors
692
707
* @returns {TestRunner }
693
708
*/
694
- function is_false ( val , msg ) {
695
- assert . ok ( ! val , `expect falsey value: ${ msg } - value: ${ JSON . stringify ( val ) } ` )
709
+ function is_false ( val , msg , response ) {
710
+ try {
711
+ assert . ok ( ( typeof val === 'string' && val . toLowerCase ( ) === 'false' ) || ! val , `expect falsey value: ${ msg } - value: ${ JSON . stringify ( val ) } ` )
712
+ } catch ( err ) {
713
+ err . response = JSON . stringify ( response )
714
+ throw err
715
+ }
696
716
}
697
717
698
718
/**
699
719
* Asserts that two values are the same
700
720
* @param {any } the first value
701
721
* @param {any } the second value
722
+ * @param {any } debugging metadata to attach to any assertion errors
702
723
* @returns {TestRunner }
703
724
*/
704
- function match ( val1 , val2 , action ) {
705
- // both values are objects
706
- if ( typeof val1 === 'object' && typeof val2 === 'object' ) {
707
- assert . deepEqual ( val1 , val2 , typeof action === 'object' ? JSON . stringify ( action ) : action )
708
- // the first value is the body as string and the second a pattern string
709
- } else if (
710
- typeof val1 === 'string' && typeof val2 === 'string' &&
711
- val2 . startsWith ( '/' ) && ( val2 . endsWith ( '/\n' ) || val2 . endsWith ( '/' ) )
712
- ) {
713
- const regStr = val2
714
- . replace ( / ( ^ | [ ^ \\ ] ) # .* / g, '$1' )
715
- . replace ( / ( ^ | [ ^ \\ ] ) \s + / g, '$1' )
716
- . slice ( 1 , - 1 )
717
- // 'm' adds the support for multiline regex
718
- assert . match ( val1 , new RegExp ( regStr , 'm' ) , `should match pattern provided: ${ val2 } , but got: ${ val1 } ` )
719
- // everything else
720
- } else {
721
- assert . equal ( val1 , val2 , `should be equal: ${ val1 } - ${ val2 } , action: ${ JSON . stringify ( action ) } ` )
725
+ function match ( val1 , val2 , action , response ) {
726
+ try {
727
+ // both values are objects
728
+ if ( typeof val1 === 'object' && typeof val2 === 'object' ) {
729
+ assert . deepEqual ( val1 , val2 , typeof action === 'object' ? JSON . stringify ( action ) : action )
730
+ // the first value is the body as string and the second a pattern string
731
+ } else if (
732
+ typeof val1 === 'string' && typeof val2 === 'string' &&
733
+ val2 . startsWith ( '/' ) && ( val2 . endsWith ( '/\n' ) || val2 . endsWith ( '/' ) )
734
+ ) {
735
+ const regStr = val2
736
+ . replace ( / ( ^ | [ ^ \\ ] ) # .* / g, '$1' )
737
+ . replace ( / ( ^ | [ ^ \\ ] ) \s + / g, '$1' )
738
+ . slice ( 1 , - 1 )
739
+ // 'm' adds the support for multiline regex
740
+ assert . match ( val1 , new RegExp ( regStr , 'm' ) , `should match pattern provided: ${ val2 } , but got: ${ val1 } : ${ JSON . stringify ( action ) } ` )
741
+ } else if ( typeof val1 === 'string' && typeof val2 === 'string' ) {
742
+ // string comparison
743
+ assert . include ( val1 , val2 , `should include pattern provided: ${ val2 } , but got: ${ val1 } : ${ JSON . stringify ( action ) } ` )
744
+ } else {
745
+ // everything else
746
+ assert . equal ( val1 , val2 , `should be equal: ${ val1 } - ${ val2 } , action: ${ JSON . stringify ( action ) } ` )
747
+ }
748
+ } catch ( err ) {
749
+ err . response = JSON . stringify ( response )
750
+ throw err
722
751
}
723
752
}
724
753
@@ -727,62 +756,92 @@ function match (val1, val2, action) {
727
756
* It also verifies that the two values are numbers
728
757
* @param {any } the first value
729
758
* @param {any } the second value
759
+ * @param {any } debugging metadata to attach to any assertion errors
730
760
* @returns {TestRunner }
731
761
*/
732
- function lt ( val1 , val2 ) {
733
- ; [ val1 , val2 ] = getNumbers ( val1 , val2 )
734
- assert . ok ( val1 < val2 )
762
+ function lt ( val1 , val2 , response ) {
763
+ try {
764
+ ; [ val1 , val2 ] = getNumbers ( val1 , val2 )
765
+ assert . ok ( val1 < val2 )
766
+ } catch ( err ) {
767
+ err . response = JSON . stringify ( response )
768
+ throw err
769
+ }
735
770
}
736
771
737
772
/**
738
773
* Asserts that the first value is greater than the second
739
774
* It also verifies that the two values are numbers
740
775
* @param {any } the first value
741
776
* @param {any } the second value
777
+ * @param {any } debugging metadata to attach to any assertion errors
742
778
* @returns {TestRunner }
743
779
*/
744
- function gt ( val1 , val2 ) {
745
- ; [ val1 , val2 ] = getNumbers ( val1 , val2 )
746
- assert . ok ( val1 > val2 )
780
+ function gt ( val1 , val2 , response ) {
781
+ try {
782
+ ; [ val1 , val2 ] = getNumbers ( val1 , val2 )
783
+ assert . ok ( val1 > val2 )
784
+ } catch ( err ) {
785
+ err . response = JSON . stringify ( response )
786
+ throw err
787
+ }
747
788
}
748
789
749
790
/**
750
791
* Asserts that the first value is less than or equal the second
751
792
* It also verifies that the two values are numbers
752
793
* @param {any } the first value
753
794
* @param {any } the second value
795
+ * @param {any } debugging metadata to attach to any assertion errors
754
796
* @returns {TestRunner }
755
797
*/
756
- function lte ( val1 , val2 ) {
757
- ; [ val1 , val2 ] = getNumbers ( val1 , val2 )
758
- assert . ok ( val1 <= val2 )
798
+ function lte ( val1 , val2 , response ) {
799
+ try {
800
+ ; [ val1 , val2 ] = getNumbers ( val1 , val2 )
801
+ assert . ok ( val1 <= val2 )
802
+ } catch ( err ) {
803
+ err . response = JSON . stringify ( response )
804
+ throw err
805
+ }
759
806
}
760
807
761
808
/**
762
809
* Asserts that the first value is greater than or equal the second
763
810
* It also verifies that the two values are numbers
764
811
* @param {any } the first value
765
812
* @param {any } the second value
813
+ * @param {any } debugging metadata to attach to any assertion errors
766
814
* @returns {TestRunner }
767
815
*/
768
- function gte ( val1 , val2 ) {
769
- ; [ val1 , val2 ] = getNumbers ( val1 , val2 )
770
- assert . ok ( val1 >= val2 )
816
+ function gte ( val1 , val2 , response ) {
817
+ try {
818
+ ; [ val1 , val2 ] = getNumbers ( val1 , val2 )
819
+ assert . ok ( val1 >= val2 )
820
+ } catch ( err ) {
821
+ err . response = JSON . stringify ( response )
822
+ throw err
823
+ }
771
824
}
772
825
773
826
/**
774
827
* Asserts that the given value has the specified length
775
828
* @param {string|object|array } the object to check
776
829
* @param {number } the expected length
830
+ * @param {any } debugging metadata to attach to any assertion errors
777
831
* @returns {TestRunner }
778
832
*/
779
- function length ( val , len ) {
780
- if ( typeof val === 'string' || Array . isArray ( val ) ) {
781
- assert . equal ( val . length , len )
782
- } else if ( typeof val === 'object' && val !== null ) {
783
- assert . equal ( Object . keys ( val ) . length , len )
784
- } else {
785
- assert . fail ( `length: the given value is invalid: ${ val } ` )
833
+ function length ( val , len , response ) {
834
+ try {
835
+ if ( typeof val === 'string' || Array . isArray ( val ) ) {
836
+ assert . equal ( val . length , len )
837
+ } else if ( typeof val === 'object' && val !== null ) {
838
+ assert . equal ( Object . keys ( val ) . length , len )
839
+ } else {
840
+ assert . fail ( `length: the given value is invalid: ${ val } ` )
841
+ }
842
+ } catch ( err ) {
843
+ err . response = JSON . stringify ( response )
844
+ throw err
786
845
}
787
846
}
788
847
@@ -813,6 +872,10 @@ function length (val, len) {
813
872
*/
814
873
function parseDo ( action ) {
815
874
action = JSON . parse ( JSON . stringify ( action ) )
875
+
876
+ if ( typeof action === 'string' ) action = { [ action ] : { } }
877
+ if ( Array . isArray ( action ) ) action = action [ 0 ]
878
+
816
879
return Object . keys ( action ) . reduce ( ( acc , val ) => {
817
880
switch ( val ) {
818
881
case 'catch' :
0 commit comments