Skip to content

Commit 8c99171

Browse files
author
Kent C. Dodds
authored
feat: log available roles when a roles query fails (#321)
Closes #286
1 parent 1f4083c commit 8c99171

File tree

5 files changed

+47
-16
lines changed

5 files changed

+47
-16
lines changed

src/__tests__/element-queries.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import jestSerializerAnsi from 'jest-serializer-ansi'
21
import {configure} from '../config'
32
import {render, renderIntoDocument} from './helpers/test-utils'
43

5-
expect.addSnapshotSerializer(jestSerializerAnsi)
64
beforeEach(() => {
75
document.defaultView.Cypress = null
86
})
@@ -88,7 +86,9 @@ test('get throws a useful error message', () => {
8886
</div>"
8987
`)
9088
expect(() => getByRole('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(`
91-
"Unable to find an element by [role=LucyRicardo]
89+
"Unable to find an element with the role "LucyRicardo"
90+
91+
Here are the available roles:
9292
9393
<div>
9494
<div />

src/__tests__/pretty-dom.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import jestSerializerAnsi from 'jest-serializer-ansi'
21
import {prettyDOM} from '../pretty-dom'
32
import {render} from './helpers/test-utils'
43

5-
expect.addSnapshotSerializer(jestSerializerAnsi)
6-
7-
test('it prints out the given DOM element tree', () => {
4+
test('prints out the given DOM element tree', () => {
85
const {container} = render('<div>Hello World!</div>')
96
expect(prettyDOM(container)).toMatchInlineSnapshot(`
107
"<div>
@@ -15,12 +12,12 @@ test('it prints out the given DOM element tree', () => {
1512
`)
1613
})
1714

18-
test('it supports truncating the output length', () => {
15+
test('supports truncating the output length', () => {
1916
const {container} = render('<div>Hello World!</div>')
2017
expect(prettyDOM(container, 5)).toMatch(/\.\.\./)
2118
})
2219

23-
test('it supports receiving the document element', () => {
20+
test('supports receiving the document element', () => {
2421
expect(prettyDOM(document)).toMatchInlineSnapshot(`
2522
"<html>
2623
<head />

src/__tests__/role.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {render} from './helpers/test-utils'
2+
3+
test('logs available roles when it fails', () => {
4+
const {getByRole} = render(`<h1>Hi</h1>`)
5+
expect(() => getByRole('article')).toThrowErrorMatchingInlineSnapshot(`
6+
"Unable to find an element with the role "article"
7+
8+
Here are the available roles:
9+
10+
heading:
11+
12+
<h1 />
13+
14+
--------------------------------------------------
15+
16+
<div>
17+
<h1>
18+
Hi
19+
</h1>
20+
</div>"
21+
`)
22+
})

src/queries/role.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {getImplicitAriaRoles} from '../role-helpers'
1+
import {getImplicitAriaRoles, prettyRoles} from '../role-helpers'
22
import {buildQueries, fuzzyMatches, makeNormalizer, matches} from './all-utils'
33

44
function queryAllByRole(
@@ -24,8 +24,18 @@ function queryAllByRole(
2424
})
2525
}
2626

27-
const getMultipleError = (c, id) => `Found multiple elements by [role=${id}]`
28-
const getMissingError = (c, id) => `Unable to find an element by [role=${id}]`
27+
const getMultipleError = (c, role) =>
28+
`Found multiple elements with the role "${role}"`
29+
const getMissingError = (container, role) =>
30+
`
31+
Unable to find an element with the role "${role}"
32+
33+
Here are the available roles:
34+
35+
${prettyRoles(container)
36+
.replace(/\n/g, '\n ')
37+
.replace(/\n\s\s\n/g, '\n\n')}
38+
`.trim()
2939

3040
const [
3141
queryByRole,

src/role-helpers.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ function getRoles(container) {
7373
}, {})
7474
}
7575

76-
function logRoles(container) {
76+
function prettyRoles(container) {
7777
const roles = getRoles(container)
7878

79-
const rolesStr = Object.entries(roles)
79+
return Object.entries(roles)
8080
.map(([role, elements]) => {
8181
const delimiterBar = '-'.repeat(50)
8282
const elementsString = elements
@@ -86,9 +86,11 @@ function logRoles(container) {
8686
return `${role}:\n\n${elementsString}\n\n${delimiterBar}`
8787
})
8888
.join('\n')
89+
}
8990

91+
function logRoles(container) {
9092
// eslint-disable-next-line no-console
91-
console.log(rolesStr)
93+
console.log(prettyRoles(container))
9294
}
9395

94-
export {getRoles, logRoles, getImplicitAriaRoles}
96+
export {getRoles, logRoles, getImplicitAriaRoles, prettyRoles}

0 commit comments

Comments
 (0)