Skip to content

Commit 9b83f01

Browse files
dolevopereps1lon
andauthored
feat: add name hint to missing error in find by queries (#1143)
Co-authored-by: Sebastian Silbermann <[email protected]>
1 parent 29a17cb commit 9b83f01

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/__tests__/role.js

+8
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,14 @@ test('has no useful error message in findBy', async () => {
363363
)
364364
})
365365

366+
test('findBy error message for missing elements contains a name hint', async () => {
367+
const {findByRole} = render(`<button>Click me</button>`)
368+
369+
await expect(findByRole('button', {name: 'Submit'})).rejects.toThrow(
370+
'Unable to find role="button" and name "Submit"',
371+
)
372+
})
373+
366374
test('explicit role is most specific', () => {
367375
const {getByRole} = renderIntoDocument(
368376
`<button role="tab" aria-label="my-tab" />`,

src/queries/role.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ function makeRoleSelector(role, exact, customNormalizer) {
220220
.join(',')
221221
}
222222

223-
const getMultipleError = (c, role, {name} = {}) => {
223+
const getNameHint = name => {
224224
let nameHint = ''
225225
if (name === undefined) {
226226
nameHint = ''
@@ -230,7 +230,11 @@ const getMultipleError = (c, role, {name} = {}) => {
230230
nameHint = ` and name \`${name}\``
231231
}
232232

233-
return `Found multiple elements with the role "${role}"${nameHint}`
233+
return nameHint
234+
}
235+
236+
const getMultipleError = (c, role, {name} = {}) => {
237+
return `Found multiple elements with the role "${role}"${getNameHint(name)}`
234238
}
235239

236240
const getMissingError = (
@@ -239,7 +243,7 @@ const getMissingError = (
239243
{hidden = getConfig().defaultHidden, name, description} = {},
240244
) => {
241245
if (getConfig()._disableExpensiveErrorDiagnostics) {
242-
return `Unable to find role="${role}"`
246+
return `Unable to find role="${role}"${getNameHint(name)}`
243247
}
244248

245249
let roles = ''

0 commit comments

Comments
 (0)