@@ -903,24 +903,152 @@ it('correctly prints diff with asymmetric matchers', () => {
903
903
}
904
904
} )
905
905
906
- it ( 'toHaveProperty error diff' , ( ) => {
907
- setupColors ( getDefaultColors ( ) )
906
+ // make it easy for dev who trims trailing whitespace on IDE
907
+ function trim ( s : string ) : string {
908
+ return s . replaceAll ( / * $ / gm, '' )
909
+ }
908
910
909
- // make it easy for dev who trims trailing whitespace on IDE
910
- function trim ( s : string ) : string {
911
- return s . replaceAll ( / * $ / gm, '' )
911
+ function getError ( f : ( ) => unknown ) {
912
+ try {
913
+ f ( )
914
+ return expect . unreachable ( )
912
915
}
913
-
914
- function getError ( f : ( ) => unknown ) {
915
- try {
916
- f ( )
917
- return expect . unreachable ( )
918
- }
919
- catch ( error ) {
920
- const processed = processError ( error )
921
- return [ processed . message , trim ( processed . diff ) ]
922
- }
916
+ catch ( error ) {
917
+ const processed = processError ( error )
918
+ return [ processed . message , trim ( processed . diff ) ]
923
919
}
920
+ }
921
+
922
+ it ( 'toMatchObject error diff' , ( ) => {
923
+ setupColors ( getDefaultColors ( ) )
924
+
925
+ // single property on root (3 total properties, 1 expected)
926
+ expect ( getError ( ( ) => expect ( { a : 1 , b : 2 , c : 3 } ) . toMatchObject ( { c : 4 } ) ) ) . toMatchInlineSnapshot ( `
927
+ [
928
+ "expected { a: 1, b: 2, c: 3 } to match object { c: 4 }
929
+ (2 matching properties omitted from actual)",
930
+ "- Expected
931
+ + Received
932
+
933
+ Object {
934
+ - "c": 4,
935
+ + "c": 3,
936
+ }",
937
+ ]
938
+ ` )
939
+
940
+ // single property on root (4 total properties, 1 expected)
941
+ expect ( getError ( ( ) => expect ( { a : 1 , b : 2 , c : { d : 4 } } ) . toMatchObject ( { b : 3 } ) ) ) . toMatchInlineSnapshot ( `
942
+ [
943
+ "expected { a: 1, b: 2, c: { d: 4 } } to match object { b: 3 }
944
+ (3 matching properties omitted from actual)",
945
+ "- Expected
946
+ + Received
947
+
948
+ Object {
949
+ - "b": 3,
950
+ + "b": 2,
951
+ }",
952
+ ]
953
+ ` )
954
+
955
+ // nested property (7 total properties, 2 expected)
956
+ expect ( getError ( ( ) => expect ( { a : 1 , b : 2 , c : { d : 4 , e : 5 } , f : { g : 6 } } ) . toMatchObject ( { c : { d : 5 } } ) ) ) . toMatchInlineSnapshot ( `
957
+ [
958
+ "expected { a: 1, b: 2, c: { d: 4, e: 5 }, …(1) } to match object { c: { d: 5 } }
959
+ (5 matching properties omitted from actual)",
960
+ "- Expected
961
+ + Received
962
+
963
+ Object {
964
+ "c": Object {
965
+ - "d": 5,
966
+ + "d": 4,
967
+ },
968
+ }",
969
+ ]
970
+ ` )
971
+
972
+ // 3 total properties, 3 expected (0 stripped)
973
+ expect ( getError ( ( ) => expect ( { a : 1 , b : 2 , c : 3 } ) . toMatchObject ( { a : 1 , b : 2 , c : 4 } ) ) ) . toMatchInlineSnapshot ( `
974
+ [
975
+ "expected { a: 1, b: 2, c: 3 } to match object { a: 1, b: 2, c: 4 }",
976
+ "- Expected
977
+ + Received
978
+
979
+ Object {
980
+ "a": 1,
981
+ "b": 2,
982
+ - "c": 4,
983
+ + "c": 3,
984
+ }",
985
+ ]
986
+ ` )
987
+
988
+ // 4 total properties, 3 expected
989
+ expect ( getError ( ( ) => expect ( { a : 1 , b : 2 , c : { d : 3 } } ) . toMatchObject ( { a : 1 , c : { d : 4 } } ) ) ) . toMatchInlineSnapshot ( `
990
+ [
991
+ "expected { a: 1, b: 2, c: { d: 3 } } to match object { a: 1, c: { d: 4 } }
992
+ (1 matching property omitted from actual)",
993
+ "- Expected
994
+ + Received
995
+
996
+ Object {
997
+ "a": 1,
998
+ "c": Object {
999
+ - "d": 4,
1000
+ + "d": 3,
1001
+ },
1002
+ }",
1003
+ ]
1004
+ ` )
1005
+
1006
+ // 8 total properties, 4 expected
1007
+ expect ( getError ( ( ) => expect ( { a : 1 , b : 2 , c : { d : 4 } , foo : { value : 'bar' } , bar : { value : 'foo' } } ) . toMatchObject ( { c : { d : 5 } , foo : { value : 'biz' } } ) ) ) . toMatchInlineSnapshot ( `
1008
+ [
1009
+ "expected { a: 1, b: 2, c: { d: 4 }, …(2) } to match object { c: { d: 5 }, foo: { value: 'biz' } }
1010
+ (4 matching properties omitted from actual)",
1011
+ "- Expected
1012
+ + Received
1013
+
1014
+ Object {
1015
+ "c": Object {
1016
+ - "d": 5,
1017
+ + "d": 4,
1018
+ },
1019
+ "foo": Object {
1020
+ - "value": "biz",
1021
+ + "value": "bar",
1022
+ },
1023
+ }",
1024
+ ]
1025
+ ` )
1026
+
1027
+ // 8 total properties, 3 expected
1028
+ const characters = { firstName : 'Vladimir' , lastName : 'Harkonnen' , family : 'House Harkonnen' , colors : [ 'red' , 'blue' ] , children : [ { firstName : 'Jessica' , lastName : 'Atreides' , colors : [ 'red' , 'green' , 'black' ] } ] }
1029
+ expect ( getError ( ( ) => expect ( characters ) . toMatchObject ( { family : 'House Atreides' , children : [ { firstName : 'Paul' } ] } ) ) ) . toMatchInlineSnapshot ( `
1030
+ [
1031
+ "expected { firstName: 'Vladimir', …(4) } to match object { family: 'House Atreides', …(1) }
1032
+ (5 matching properties omitted from actual)",
1033
+ "- Expected
1034
+ + Received
1035
+
1036
+ Object {
1037
+ "children": Array [
1038
+ Object {
1039
+ - "firstName": "Paul",
1040
+ + "firstName": "Jessica",
1041
+ },
1042
+ ],
1043
+ - "family": "House Atreides",
1044
+ + "family": "House Harkonnen",
1045
+ }",
1046
+ ]
1047
+ ` )
1048
+ } )
1049
+
1050
+ it ( 'toHaveProperty error diff' , ( ) => {
1051
+ setupColors ( getDefaultColors ( ) )
924
1052
925
1053
// non match value
926
1054
expect ( getError ( ( ) => expect ( { name : 'foo' } ) . toHaveProperty ( 'name' , 'bar' ) ) ) . toMatchInlineSnapshot ( `
0 commit comments