Skip to content

Commit 639294c

Browse files
authored
feat: lists elements that matched multiple error (#678)
* Lists elements that matched multiple error * Lifts suggestion printing up one level * Tests matching elements in multiple found error * Adds accessible name to error message
1 parent 3c262fd commit 639294c

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

src/__tests__/role.js

+70
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,20 @@ test('accessible regex name in error message for multiple found', () => {
392392
.toThrowErrorMatchingInlineSnapshot(`
393393
"Found multiple elements with the role "button" and name \`/value/i\`
394394
395+
Here are the matching elements:
396+
397+
<button>
398+
Increment value
399+
</button>
400+
401+
<button>
402+
Decrement value
403+
</button>
404+
405+
<button>
406+
Reset value
407+
</button>
408+
395409
(If this is intentional, then use the \`*AllBy*\` variant of the query (like \`queryAllByText\`, \`getAllByText\`, or \`findAllByText\`)).
396410
397411
<div>
@@ -420,6 +434,20 @@ test('accessible string name in error message for multiple found', () => {
420434
.toThrowErrorMatchingInlineSnapshot(`
421435
"Found multiple elements with the role "button" and name "Submit"
422436
437+
Here are the matching elements:
438+
439+
<button>
440+
Submit
441+
</button>
442+
443+
<button>
444+
Submit
445+
</button>
446+
447+
<button>
448+
Submit
449+
</button>
450+
423451
(If this is intentional, then use the \`*AllBy*\` variant of the query (like \`queryAllByText\`, \`getAllByText\`, or \`findAllByText\`)).
424452
425453
<div>
@@ -436,6 +464,48 @@ test('accessible string name in error message for multiple found', () => {
436464
`)
437465
})
438466

467+
test('matching elements in error for multiple found', () => {
468+
const {getByRole} = render(
469+
`<button>Increment value</button
470+
><button>Different label</button
471+
><p>Wrong role</p
472+
><button>Reset value</button
473+
>`,
474+
)
475+
476+
expect(() => getByRole('button', {name: /value/i}))
477+
.toThrowErrorMatchingInlineSnapshot(`
478+
"Found multiple elements with the role "button" and name \`/value/i\`
479+
480+
Here are the matching elements:
481+
482+
<button>
483+
Increment value
484+
</button>
485+
486+
<button>
487+
Reset value
488+
</button>
489+
490+
(If this is intentional, then use the \`*AllBy*\` variant of the query (like \`queryAllByText\`, \`getAllByText\`, or \`findAllByText\`)).
491+
492+
<div>
493+
<button>
494+
Increment value
495+
</button>
496+
<button>
497+
Different label
498+
</button>
499+
<p>
500+
Wrong role
501+
</p>
502+
<button>
503+
Reset value
504+
</button>
505+
</div>"
506+
`)
507+
})
508+
439509
describe('configuration', () => {
440510
let originalConfig
441511
beforeEach(() => {

src/query-helpers.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {prettyDOM} from './pretty-dom'
12
import {getSuggestedQuery} from './suggestions'
23
import {fuzzyMatches, matches, makeNormalizer} from './matches'
34
import {waitFor} from './wait-for'
@@ -45,8 +46,14 @@ function makeSingleQuery(allQuery, getMultipleError) {
4546
return (container, ...args) => {
4647
const els = allQuery(container, ...args)
4748
if (els.length > 1) {
49+
const elementStrings = els.map(element => prettyDOM(element)).join('\n\n')
50+
4851
throw getMultipleElementsFoundError(
49-
getMultipleError(container, ...args),
52+
`${getMultipleError(container, ...args)}
53+
54+
Here are the matching elements:
55+
56+
${elementStrings}`,
5057
container,
5158
)
5259
}

0 commit comments

Comments
 (0)