@@ -29,16 +29,16 @@ test('query can return null', () => {
2929} )
3030
3131describe ( 'get throws a useful error message' , ( ) => {
32- let renderResult
32+ let missingRenderResult ;
3333
3434 beforeEach ( ( ) => {
35- renderResult = render (
35+ missingRenderResult = render (
3636 `<div></div><!-- Ignored comment --><style type="text/css">body {} </style><script type="text/javascript></script>` ,
3737 )
3838 } )
3939
4040 test ( 'ByLabelText' , ( ) => {
41- const { getByLabelText} = renderResult
41+ const { getByLabelText} = missingRenderResult
4242
4343 expect ( ( ) => getByLabelText ( 'LucyRicardo' ) )
4444 . toThrowErrorMatchingInlineSnapshot ( `
@@ -52,12 +52,22 @@ describe('get throws a useful error message', () => {
5252 } )
5353
5454 test ( 'ByPlaceholderText' , ( ) => {
55- const { getByPlaceholderText} = renderResult
55+ const { getByPlaceholderText} = missingRenderResult
5656
5757 expect ( ( ) => getByPlaceholderText ( 'LucyRicardo' ) )
5858 . toThrowErrorMatchingInlineSnapshot ( `
5959 Unable to find an element with the placeholder text of: LucyRicardo
6060
61+ Ignored nodes: comments, script, style
62+ <div>
63+ <div />
64+ </div>
65+ ` )
66+
67+ expect ( ( ) => getByPlaceholderText ( ' LucyRicardo ' ) )
68+ . toThrowErrorMatchingInlineSnapshot ( `
69+ Unable to find an element with the placeholder text of: LucyRicardo (normalized from ' LucyRicardo ')
70+
6171 Ignored nodes: comments, script, style
6272 <div>
6373 <div />
@@ -100,10 +110,11 @@ describe('get throws a useful error message', () => {
100110 ` )
101111 } )
102112
103- test ( 'ByText' , ( ) => {
104- const { getByText} = renderResult
113+ describe ( 'ByText' , ( ) => {
114+ test ( 'Missing element' , ( ) => {
115+ const { getByText} = missingRenderResult
105116
106- expect ( ( ) => getByText ( 'LucyRicardo' ) ) . toThrowErrorMatchingInlineSnapshot ( `
117+ expect ( ( ) => getByText ( 'LucyRicardo' ) ) . toThrowErrorMatchingInlineSnapshot ( `
107118 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.
108119
109120 Ignored nodes: comments, script, style
@@ -112,8 +123,8 @@ describe('get throws a useful error message', () => {
112123 </div>
113124 ` )
114125
115- expect ( ( ) => getByText ( 'LucyRicardo' , { selector : 'span' } ) )
116- . toThrowErrorMatchingInlineSnapshot ( `
126+ expect ( ( ) => getByText ( 'LucyRicardo' , { selector : 'span' } ) )
127+ . toThrowErrorMatchingInlineSnapshot ( `
117128 Unable to find an element with the text: LucyRicardo, which matches selector 'span'. 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.
118129
119130 Ignored nodes: comments, script, style
@@ -122,7 +133,16 @@ describe('get throws a useful error message', () => {
122133 </div>
123134 ` )
124135
125- expect ( ( ) => getByText ( / L u c y R i c a r d o / ) ) . toThrowErrorMatchingInlineSnapshot ( `
136+ expect ( ( ) => getByText ( ' LucyRicardo ' , { } ) ) . toThrowErrorMatchingInlineSnapshot ( `
137+ Unable to find an element with the text: LucyRicardo (normalized from ' 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.
138+
139+ Ignored nodes: comments, script, style
140+ <div>
141+ <div />
142+ </div>
143+ ` )
144+
145+ expect ( ( ) => getByText ( / L u c y R i c a r d o / ) ) . toThrowErrorMatchingInlineSnapshot ( `
126146 Unable to find an element that its text match the regex: /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.
127147
128148 Ignored nodes: comments, script, style
@@ -131,7 +151,7 @@ describe('get throws a useful error message', () => {
131151 </div>
132152 ` )
133153
134- expect ( ( ) => getByText ( ( ) => false ) ) . toThrowErrorMatchingInlineSnapshot ( `
154+ expect ( ( ) => getByText ( ( ) => false ) ) . toThrowErrorMatchingInlineSnapshot ( `
135155 Unable to find an element that match the custom matcher: [anonymous function]. 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.
136156
137157 Ignored nodes: comments, script, style
@@ -140,23 +160,87 @@ describe('get throws a useful error message', () => {
140160 </div>
141161 ` )
142162
143- function something ( ) {
144- return false
145- }
146- something . customMatcherText = 'Lucy and Ricardo'
163+ function something ( ) {
164+ return false
165+ }
166+ something . customMatcherText = 'Lucy and Ricardo'
147167
148- expect ( ( ) => getByText ( something ) ) . toThrowErrorMatchingInlineSnapshot ( `
168+ expect ( ( ) => getByText ( something ) ) . toThrowErrorMatchingInlineSnapshot ( `
149169 Unable to find an element that match the custom matcher: Lucy and Ricardo. 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.
150170
151171 Ignored nodes: comments, script, style
152172 <div>
153173 <div />
154174 </div>
155175 ` )
156- } )
176+ } ) ;
177+
178+ test ( 'Multiple elements' , ( ) => {
179+ const { getByText} = render (
180+ `<div><span>LucyRicardo</span><span>LucyRicardo</span></div><!-- Ignored comment --><style type="text/css">body {} </style><script type="text/javascript></script>` ,
181+ ) ;
182+
183+ expect ( ( ) => getByText ( 'LucyRicardo' ) ) . toThrowErrorMatchingInlineSnapshot ( `Found multiple elements with the text: LucyRicardo
184+
185+ Here are the matching elements:
186+
187+ Ignored nodes: comments, script, style
188+ <span>
189+ LucyRicardo
190+ </span>
191+
192+ Ignored nodes: comments, script, style
193+ <span>
194+ LucyRicardo
195+ </span>
196+
197+ (If this is intentional, then use the \`*AllBy*\` variant of the query (like \`queryAllByText\`, \`getAllByText\`, or \`findAllByText\`)).
198+
199+ Ignored nodes: comments, script, style
200+ <div>
201+ <div>
202+ <span>
203+ LucyRicardo
204+ </span>
205+ <span>
206+ LucyRicardo
207+ </span>
208+ </div>
209+ </div>` ) ;
210+
211+ expect ( ( ) => getByText ( 'LucyRicardo' , { selector : 'span' } ) )
212+ . toThrowErrorMatchingInlineSnapshot ( `Found multiple elements with the text: LucyRicardo, which matches selector 'span'
213+
214+ Here are the matching elements:
215+
216+ Ignored nodes: comments, script, style
217+ <span>
218+ LucyRicardo
219+ </span>
220+
221+ Ignored nodes: comments, script, style
222+ <span>
223+ LucyRicardo
224+ </span>
225+
226+ (If this is intentional, then use the \`*AllBy*\` variant of the query (like \`queryAllByText\`, \`getAllByText\`, or \`findAllByText\`)).
227+
228+ Ignored nodes: comments, script, style
229+ <div>
230+ <div>
231+ <span>
232+ LucyRicardo
233+ </span>
234+ <span>
235+ LucyRicardo
236+ </span>
237+ </div>
238+ </div>` )
239+ } ) ;
240+ } ) ;
157241
158242 test ( 'ByTestId' , ( ) => {
159- const { getByTestId} = renderResult
243+ const { getByTestId} = missingRenderResult
160244
161245 expect ( ( ) => getByTestId ( 'LucyRicardo' ) )
162246 . toThrowErrorMatchingInlineSnapshot ( `
@@ -170,12 +254,22 @@ describe('get throws a useful error message', () => {
170254 } )
171255
172256 test ( 'ByAltText' , ( ) => {
173- const { getByAltText} = renderResult
257+ const { getByAltText} = missingRenderResult
174258
175259 expect ( ( ) => getByAltText ( 'LucyRicardo' ) )
176260 . toThrowErrorMatchingInlineSnapshot ( `
177261 Unable to find an element with the alt text: LucyRicardo
178262
263+ Ignored nodes: comments, script, style
264+ <div>
265+ <div />
266+ </div>
267+ ` )
268+
269+ expect ( ( ) => getByAltText ( ' LucyRicardo ' ) )
270+ . toThrowErrorMatchingInlineSnapshot ( `
271+ Unable to find an element with the alt text: LucyRicardo (normalized from ' LucyRicardo ')
272+
179273 Ignored nodes: comments, script, style
180274 <div>
181275 <div />
@@ -217,11 +311,20 @@ describe('get throws a useful error message', () => {
217311 } )
218312
219313 test ( 'ByTitle' , ( ) => {
220- const { getByTitle} = renderResult
314+ const { getByTitle} = missingRenderResult
221315
222316 expect ( ( ) => getByTitle ( 'LucyRicardo' ) ) . toThrowErrorMatchingInlineSnapshot ( `
223317 Unable to find an element with the title: LucyRicardo.
224318
319+ Ignored nodes: comments, script, style
320+ <div>
321+ <div />
322+ </div>
323+ ` )
324+
325+ expect ( ( ) => getByTitle ( ' LucyRicardo ' ) ) . toThrowErrorMatchingInlineSnapshot ( `
326+ Unable to find an element with the title: LucyRicardo (normalized from ' LucyRicardo ').
327+
225328 Ignored nodes: comments, script, style
226329 <div>
227330 <div />
@@ -262,12 +365,22 @@ describe('get throws a useful error message', () => {
262365 } )
263366
264367 test ( 'ByDisplayValue' , ( ) => {
265- const { getByDisplayValue} = renderResult
368+ const { getByDisplayValue} = missingRenderResult
266369
267370 expect ( ( ) => getByDisplayValue ( 'LucyRicardo' ) )
268371 . toThrowErrorMatchingInlineSnapshot ( `
269372 Unable to find an element with the display value: LucyRicardo.
270373
374+ Ignored nodes: comments, script, style
375+ <div>
376+ <div />
377+ </div>
378+ ` )
379+
380+ expect ( ( ) => getByDisplayValue ( ' LucyRicardo ' ) )
381+ . toThrowErrorMatchingInlineSnapshot ( `
382+ Unable to find an element with the display value: LucyRicardo (normalized from ' LucyRicardo ').
383+
271384 Ignored nodes: comments, script, style
272385 <div>
273386 <div />
@@ -311,7 +424,7 @@ describe('get throws a useful error message', () => {
311424 } )
312425
313426 test ( 'ByRole' , ( ) => {
314- const { getByRole} = renderResult
427+ const { getByRole} = missingRenderResult
315428
316429 expect ( ( ) => getByRole ( 'LucyRicardo' ) ) . toThrowErrorMatchingInlineSnapshot ( `
317430 Unable to find an accessible element with the role "LucyRicardo"
0 commit comments