Skip to content

Commit 9cc7ce9

Browse files
author
davidsa
committed
feat(config): Add showLogOnfail config option to configure logging
1 parent cf57dcd commit 9cc7ce9

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`getByLabelText query will throw expected error message when config.showLogOnFail is false 1`] = `"Unable to find a label with the text of: TEST QUERY"`;
4+
5+
exports[`getByLabelText query will throw expected error message when config.showLogOnFail is true 1`] = `
6+
"Unable to find a label with the text of: TEST QUERY
7+
8+
<body>
9+
<div>
10+
Hello
11+
</div>
12+
</body>"
13+
`;
14+
15+
exports[`getByText query will throw expected error message when config.showLogOnFail is false 1`] = `"Unable to find an element with the text: TEST QUERY. 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."`;
16+
17+
exports[`getByText query will throw expected error message when config.showLogOnFail is true 1`] = `
18+
"Unable to find an element with the text: TEST QUERY. 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.
19+
20+
<body>
21+
<div>
22+
Hello
23+
</div>
24+
</body>"
25+
`;

src/__tests__/get-by-errors.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import cases from 'jest-in-case'
2+
import {screen} from '../'
3+
import {configure, getConfig} from '../config'
24
import {render} from './helpers/test-utils'
35

6+
const originalConfig = getConfig()
7+
beforeEach(() => {
8+
configure(originalConfig)
9+
})
10+
411
cases(
512
'getBy* queries throw an error when there are multiple elements returned',
613
({name, query, html}) => {
@@ -39,6 +46,20 @@ cases(
3946
},
4047
)
4148

49+
test.each([
50+
['getByText', true],
51+
['getByText', false],
52+
['getByLabelText', true],
53+
['getByLabelText', false],
54+
])(
55+
'%s query will throw expected error message when config.showLogOnFail is %s',
56+
(query, showLogOnFail) => {
57+
configure({showLogOnFail})
58+
document.body.innerHTML = '<div>Hello</div>'
59+
expect(() => screen[query]('TEST QUERY')).toThrowErrorMatchingSnapshot()
60+
},
61+
)
62+
4263
cases(
4364
'queryBy* queries throw an error when there are multiple elements returned',
4465
({name, query, html}) => {

src/config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ let config = {
1414
asyncWrapper: cb => cb(),
1515
// default value for the `hidden` option in `ByRole` queries
1616
defaultHidden: false,
17+
18+
// should the DOM be logged to the console when getBy* or getAllBy* query fail
19+
showLogOnFail: true,
1720
}
1821

1922
export function configure(newConfig) {

src/query-helpers.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import {prettyDOM} from './pretty-dom'
22
import {fuzzyMatches, matches, makeNormalizer} from './matches'
33
import {waitForElement} from './wait-for-element'
4+
import {getConfig} from './config'
45

56
function getElementError(message, container) {
6-
return new Error([message, prettyDOM(container)].filter(Boolean).join('\n\n'))
7+
const errorMessages = getConfig().showLogOnFail
8+
? [message, prettyDOM(container)]
9+
: [message]
10+
return new Error(errorMessages.filter(Boolean).join('\n\n'))
711
}
812

913
function getMultipleElementsFoundError(message, container) {

0 commit comments

Comments
 (0)