@@ -34,17 +34,72 @@ test('get throws a useful error message', () => {
34
34
getByValue,
35
35
getByRole,
36
36
} = render ( '<div />' )
37
- expect ( ( ) => getByLabelText ( 'LucyRicardo' ) ) . toThrowErrorMatchingSnapshot ( )
38
- expect ( ( ) => getBySelectText ( 'LucyRicardo' ) ) . toThrowErrorMatchingSnapshot ( )
39
- expect ( ( ) =>
40
- getByPlaceholderText ( 'LucyRicardo' ) ,
41
- ) . toThrowErrorMatchingSnapshot ( )
42
- expect ( ( ) => getByText ( 'LucyRicardo' ) ) . toThrowErrorMatchingSnapshot ( )
43
- expect ( ( ) => getByTestId ( 'LucyRicardo' ) ) . toThrowErrorMatchingSnapshot ( )
44
- expect ( ( ) => getByAltText ( 'LucyRicardo' ) ) . toThrowErrorMatchingSnapshot ( )
45
- expect ( ( ) => getByTitle ( 'LucyRicardo' ) ) . toThrowErrorMatchingSnapshot ( )
46
- expect ( ( ) => getByValue ( 'LucyRicardo' ) ) . toThrowErrorMatchingSnapshot ( )
47
- expect ( ( ) => getByRole ( 'LucyRicardo' ) ) . toThrowErrorMatchingSnapshot ( )
37
+ expect ( ( ) => getByLabelText ( 'LucyRicardo' ) )
38
+ . toThrowErrorMatchingInlineSnapshot ( `
39
+ "Unable to find a label with the text of: LucyRicardo
40
+
41
+ [36m<div>[39m
42
+ [36m<div />[39m
43
+ [36m</div>[39m"
44
+ ` )
45
+ expect ( ( ) => getBySelectText ( 'LucyRicardo' ) )
46
+ . toThrowErrorMatchingInlineSnapshot ( `
47
+ "Unable to find a <select> element with the selected option's text: LucyRicardo
48
+
49
+ [36m<div>[39m
50
+ [36m<div />[39m
51
+ [36m</div>[39m"
52
+ ` )
53
+ expect ( ( ) => getByPlaceholderText ( 'LucyRicardo' ) )
54
+ . toThrowErrorMatchingInlineSnapshot ( `
55
+ "Unable to find an element with the placeholder text of: LucyRicardo
56
+
57
+ [36m<div>[39m
58
+ [36m<div />[39m
59
+ [36m</div>[39m"
60
+ ` )
61
+ expect ( ( ) => getByText ( 'LucyRicardo' ) ) . toThrowErrorMatchingInlineSnapshot ( `
62
+ "Unable to find an element with the text: LucyRicardo. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.
63
+
64
+ [36m<div>[39m
65
+ [36m<div />[39m
66
+ [36m</div>[39m"
67
+ ` )
68
+ expect ( ( ) => getByTestId ( 'LucyRicardo' ) ) . toThrowErrorMatchingInlineSnapshot ( `
69
+ "Unable to find an element by: [data-testid=\\"LucyRicardo\\"]
70
+
71
+ [36m<div>[39m
72
+ [36m<div />[39m
73
+ [36m</div>[39m"
74
+ ` )
75
+ expect ( ( ) => getByAltText ( 'LucyRicardo' ) ) . toThrowErrorMatchingInlineSnapshot ( `
76
+ "Unable to find an element with the alt text: LucyRicardo
77
+
78
+ [36m<div>[39m
79
+ [36m<div />[39m
80
+ [36m</div>[39m"
81
+ ` )
82
+ expect ( ( ) => getByTitle ( 'LucyRicardo' ) ) . toThrowErrorMatchingInlineSnapshot ( `
83
+ "Unable to find an element with the title: LucyRicardo.
84
+
85
+ [36m<div>[39m
86
+ [36m<div />[39m
87
+ [36m</div>[39m"
88
+ ` )
89
+ expect ( ( ) => getByValue ( 'LucyRicardo' ) ) . toThrowErrorMatchingInlineSnapshot ( `
90
+ "Unable to find an element with the value: LucyRicardo.
91
+
92
+ [36m<div>[39m
93
+ [36m<div />[39m
94
+ [36m</div>[39m"
95
+ ` )
96
+ expect ( ( ) => getByRole ( 'LucyRicardo' ) ) . toThrowErrorMatchingInlineSnapshot ( `
97
+ "Unable to find an element by role=LucyRicardo
98
+
99
+ [36m<div>[39m
100
+ [36m<div />[39m
101
+ [36m</div>[39m"
102
+ ` )
48
103
} )
49
104
50
105
test ( 'can get elements by matching their text content' , ( ) => {
@@ -160,13 +215,27 @@ test('get can get form controls by placeholder', () => {
160
215
test ( 'label with no form control' , ( ) => {
161
216
const { getByLabelText, queryByLabelText} = render ( `<label>All alone</label>` )
162
217
expect ( queryByLabelText ( / a l o n e / ) ) . toBeNull ( )
163
- expect ( ( ) => getByLabelText ( / a l o n e / ) ) . toThrowErrorMatchingSnapshot ( )
218
+ expect ( ( ) => getByLabelText ( / a l o n e / ) ) . toThrowErrorMatchingInlineSnapshot ( `
219
+ "Found a label with the text of: /alone/, however no form control was found associated to that label. Make sure you're using the \\"for\\" attribute or \\"aria-labelledby\\" attribute correctly.
220
+
221
+ [36m<div>[39m
222
+ [36m<label>[39m
223
+ [0mAll alone[0m
224
+ [36m</label>[39m
225
+ [36m</div>[39m"
226
+ ` )
164
227
} )
165
228
166
229
test ( 'totally empty label' , ( ) => {
167
230
const { getByLabelText, queryByLabelText} = render ( `<label />` )
168
231
expect ( queryByLabelText ( '' ) ) . toBeNull ( )
169
- expect ( ( ) => getByLabelText ( '' ) ) . toThrowErrorMatchingSnapshot ( )
232
+ expect ( ( ) => getByLabelText ( '' ) ) . toThrowErrorMatchingInlineSnapshot ( `
233
+ "Found a label with the text of: , however no form control was found associated to that label. Make sure you're using the \\"for\\" attribute or \\"aria-labelledby\\" attribute correctly.
234
+
235
+ [36m<div>[39m
236
+ [36m<label />[39m
237
+ [36m</div>[39m"
238
+ ` )
170
239
} )
171
240
172
241
test ( 'getByLabelText with aria-label' , ( ) => {
@@ -568,16 +637,36 @@ test('get throws a useful error message without DOM in Cypress', () => {
568
637
getByTitle,
569
638
getByValue,
570
639
} = render ( '<div />' )
571
- expect ( ( ) => getByLabelText ( 'LucyRicardo' ) ) . toThrowErrorMatchingSnapshot ( )
572
- expect ( ( ) => getBySelectText ( 'LucyRicardo' ) ) . toThrowErrorMatchingSnapshot ( )
640
+ expect ( ( ) =>
641
+ getByLabelText ( 'LucyRicardo' ) ,
642
+ ) . toThrowErrorMatchingInlineSnapshot (
643
+ `"Unable to find a label with the text of: LucyRicardo"` ,
644
+ )
645
+ expect ( ( ) =>
646
+ getBySelectText ( 'LucyRicardo' ) ,
647
+ ) . toThrowErrorMatchingInlineSnapshot (
648
+ `"Unable to find a <select> element with the selected option's text: LucyRicardo"` ,
649
+ )
573
650
expect ( ( ) =>
574
651
getByPlaceholderText ( 'LucyRicardo' ) ,
575
- ) . toThrowErrorMatchingSnapshot ( )
576
- expect ( ( ) => getByText ( 'LucyRicardo' ) ) . toThrowErrorMatchingSnapshot ( )
577
- expect ( ( ) => getByTestId ( 'LucyRicardo' ) ) . toThrowErrorMatchingSnapshot ( )
578
- expect ( ( ) => getByAltText ( 'LucyRicardo' ) ) . toThrowErrorMatchingSnapshot ( )
579
- expect ( ( ) => getByTitle ( 'LucyRicardo' ) ) . toThrowErrorMatchingSnapshot ( )
580
- expect ( ( ) => getByValue ( 'LucyRicardo' ) ) . toThrowErrorMatchingSnapshot ( )
652
+ ) . toThrowErrorMatchingInlineSnapshot (
653
+ `"Unable to find an element with the placeholder text of: LucyRicardo"` ,
654
+ )
655
+ expect ( ( ) => getByText ( 'LucyRicardo' ) ) . toThrowErrorMatchingInlineSnapshot (
656
+ `"Unable to find an element with the text: LucyRicardo. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible."` ,
657
+ )
658
+ expect ( ( ) => getByTestId ( 'LucyRicardo' ) ) . toThrowErrorMatchingInlineSnapshot (
659
+ `"Unable to find an element by: [data-testid=\\"LucyRicardo\\"]"` ,
660
+ )
661
+ expect ( ( ) => getByAltText ( 'LucyRicardo' ) ) . toThrowErrorMatchingInlineSnapshot (
662
+ `"Unable to find an element with the alt text: LucyRicardo"` ,
663
+ )
664
+ expect ( ( ) => getByTitle ( 'LucyRicardo' ) ) . toThrowErrorMatchingInlineSnapshot (
665
+ `"Unable to find an element with the title: LucyRicardo."` ,
666
+ )
667
+ expect ( ( ) => getByValue ( 'LucyRicardo' ) ) . toThrowErrorMatchingInlineSnapshot (
668
+ `"Unable to find an element with the value: LucyRicardo."` ,
669
+ )
581
670
} )
582
671
583
672
test ( 'getByText ignores script tags by default' , ( ) => {
0 commit comments