Skip to content

Commit 1be0482

Browse files
author
davidsa
committed
feat(config): Add customGetElementError config option
closes #360
1 parent cf57dcd commit 1be0482

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`getByLabelText query will throw the custom error returned by config.customGetElementError 1`] = `"My custom error: Unable to find a label with the text of: TEST QUERY"`;
4+
5+
exports[`getByText query will throw the custom error returned by config.customGetElementError 1`] = `"My custom error: 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."`;

src/__tests__/get-by-errors.js

Lines changed: 20 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,19 @@ cases(
3946
},
4047
)
4148

49+
test.each([['getByText'], ['getByLabelText']])(
50+
'%s query will throw the custom error returned by config.customGetElementError',
51+
query => {
52+
const customGetElementError = jest.fn(
53+
(message, _container) => new Error(`My custom error: ${message}`),
54+
)
55+
configure({customGetElementError})
56+
document.body.innerHTML = '<div>Hello</div>'
57+
expect(() => screen[query]('TEST QUERY')).toThrowErrorMatchingSnapshot()
58+
expect(customGetElementError).toBeCalledTimes(1)
59+
},
60+
)
61+
4262
cases(
4363
'queryBy* queries throw an error when there are multiple elements returned',
4464
({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+
// called when getBy* queries fail. (message, container) => Error
19+
customGetElementError: null,
1720
}
1821

1922
export function configure(newConfig) {

src/query-helpers.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
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 customGetElementError = getConfig().customGetElementError
8+
const errorMessages = [message, prettyDOM(container)]
9+
if (customGetElementError) {
10+
return customGetElementError(...errorMessages)
11+
}
12+
return new Error(errorMessages.filter(Boolean).join('\n\n'))
713
}
814

915
function getMultipleElementsFoundError(message, container) {

0 commit comments

Comments
 (0)