diff --git a/src/__tests__/__snapshots__/get-by-errors.js.snap b/src/__tests__/__snapshots__/get-by-errors.js.snap
index 45f4cde1..fa3e27e0 100644
--- a/src/__tests__/__snapshots__/get-by-errors.js.snap
+++ b/src/__tests__/__snapshots__/get-by-errors.js.snap
@@ -2,4 +2,4 @@
exports[`getByLabelText query will throw the custom error returned by config.getElementError 1`] = `"My custom error: Unable to find a label with the text of: TEST QUERY"`;
-exports[`getByText query will throw the custom error returned by config.getElementError 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."`;
+exports[`getByText query will throw the custom error returned by config.getElementError 1`] = `"My custom error: Unable to find an element using the provided matcher.The matcher is - 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."`;
diff --git a/src/__tests__/element-queries.js b/src/__tests__/element-queries.js
index 3e3ee6de..dd100c42 100644
--- a/src/__tests__/element-queries.js
+++ b/src/__tests__/element-queries.js
@@ -35,65 +35,65 @@ test('get throws a useful error message', () => {
} = render('
')
expect(() => getByLabelText('LucyRicardo'))
.toThrowErrorMatchingInlineSnapshot(`
-"Unable to find a label with the text of: LucyRicardo
+ "Unable to find a label with the text of: LucyRicardo
-
-
-
"
-`)
+
+
+
"
+ `)
expect(() => getByPlaceholderText('LucyRicardo'))
.toThrowErrorMatchingInlineSnapshot(`
-"Unable to find an element with the placeholder text of: LucyRicardo
+ "Unable to find an element with the placeholder text of: LucyRicardo
-
-
-
"
-`)
+
+
+
"
+ `)
expect(() => getByText('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(`
-"Unable to find an element with the text: LucyRicardo. 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.
+ "Unable to find an element using the provided matcher.The matcher is - LucyRicardo 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.
-
-
-
"
-`)
+
+
+
"
+ `)
expect(() => getByTestId('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(`
-"Unable to find an element by: [data-testid="LucyRicardo"]
+ "Unable to find an element by: [data-testid="LucyRicardo"]
-
-
-
"
-`)
+
+
+
"
+ `)
expect(() => getByAltText('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(`
-"Unable to find an element with the alt text: LucyRicardo
+ "Unable to find an element with the alt text: LucyRicardo
-
-
-
"
-`)
+
+
+
"
+ `)
expect(() => getByTitle('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(`
-"Unable to find an element with the title: LucyRicardo.
+ "Unable to find an element with the title: LucyRicardo.
-
-
-
"
-`)
+
+
+
"
+ `)
expect(() => getByDisplayValue('LucyRicardo'))
.toThrowErrorMatchingInlineSnapshot(`
-"Unable to find an element with the display value: LucyRicardo.
+ "Unable to find an element with the display value: LucyRicardo.
-
-
-
"
-`)
+
+
+
"
+ `)
expect(() => getByRole('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(`
-"Unable to find an accessible element with the role "LucyRicardo"
+ "Unable to find an accessible element with the role "LucyRicardo"
-There are no accessible roles. But there might be some inaccessible roles. If you wish to access them, then set the \`hidden\` option to \`true\`. Learn more about this here: https://testing-library.com/docs/dom-testing-library/api-queries#byrole
+ There are no accessible roles. But there might be some inaccessible roles. If you wish to access them, then set the \`hidden\` option to \`true\`. Learn more about this here: https://testing-library.com/docs/dom-testing-library/api-queries#byrole
-
-
-
"
-`)
+
+
+
"
+ `)
})
test('can get elements by matching their text content', () => {
@@ -352,14 +352,14 @@ test('label with no form control', () => {
const {getByLabelText, queryByLabelText} = render(``)
expect(queryByLabelText(/alone/)).toBeNull()
expect(() => getByLabelText(/alone/)).toThrowErrorMatchingInlineSnapshot(`
-"Found a label with the text of: /alone/, however no form control was found associated to that label. Make sure you're using the "for" attribute or "aria-labelledby" attribute correctly.
+ "Found a label with the text of: /alone/, however no form control was found associated to that label. Make sure you're using the "for" attribute or "aria-labelledby" attribute correctly.
-
-
-
"
-`)
+
+
+
"
+ `)
})
test('label with "for" attribute but no form control and fuzzy matcher', () => {
@@ -369,16 +369,16 @@ test('label with "for" attribute but no form control and fuzzy matcher', () => {
expect(queryByLabelText('alone', {exact: false})).toBeNull()
expect(() => getByLabelText('alone', {exact: false}))
.toThrowErrorMatchingInlineSnapshot(`
-"Found a label with the text of: alone, however no form control was found associated to that label. Make sure you're using the "for" attribute or "aria-labelledby" attribute correctly.
-
-
-
-
"
-`)
+ "Found a label with the text of: alone, however no form control was found associated to that label. Make sure you're using the "for" attribute or "aria-labelledby" attribute correctly.
+
+
+
+
"
+ `)
})
test('label with children with no form control', () => {
@@ -391,32 +391,32 @@ test('label with children with no form control', () => {
expect(queryByLabelText(/alone/, {selector: 'input'})).toBeNull()
expect(() => getByLabelText(/alone/, {selector: 'input'}))
.toThrowErrorMatchingInlineSnapshot(`
-"Found a label with the text of: /alone/, however no form control was found associated to that label. Make sure you're using the "for" attribute or "aria-labelledby" attribute correctly.
+ "Found a label with the text of: /alone/, however no form control was found associated to that label. Make sure you're using the "for" attribute or "aria-labelledby" attribute correctly.
-
-
-
-
-
"
-`)
+
+
+
+
+
"
+ `)
})
test('label with non-labellable element', () => {
@@ -431,35 +431,35 @@ test('label with non-labellable element', () => {
expect(queryByLabelText(/Label/)).toBeNull()
expect(() => getByLabelText(/Label/)).toThrowErrorMatchingInlineSnapshot(`
-"Found a label with the text of: /Label/, however the element associated with this label () is non-labellable [https://html.spec.whatwg.org/multipage/forms.html#category-label]. If you really need to label a , you can use aria-label or aria-labelledby instead.
+ "Found a label with the text of: /Label/, however the element associated with this label () is non-labellable [https://html.spec.whatwg.org/multipage/forms.html#category-label]. If you really need to label a , you can use aria-label or aria-labelledby instead.
-
-
-
-
-
-
-
-
-
-
+
- Hello
-
-
-
-
-
-
-
-
"
-`)
+
+
+
+
+
+
+
+
+
+ Hello
+
+
+
+
+
+
+
+
"
+ `)
})
test('multiple labels with non-labellable elements', () => {
@@ -478,65 +478,65 @@ test('multiple labels with non-labellable elements', () => {
expect(queryAllByLabelText(/Label/)).toEqual([])
expect(() => getAllByLabelText(/Label/)).toThrowErrorMatchingInlineSnapshot(`
-"Found a label with the text of: /Label/, however the element associated with this label () is non-labellable [https://html.spec.whatwg.org/multipage/forms.html#category-label]. If you really need to label a , you can use aria-label or aria-labelledby instead.
+ "Found a label with the text of: /Label/, however the element associated with this label () is non-labellable [https://html.spec.whatwg.org/multipage/forms.html#category-label]. If you really need to label a , you can use aria-label or aria-labelledby instead.
-Found a label with the text of: /Label/, however the element associated with this label () is non-labellable [https://html.spec.whatwg.org/multipage/forms.html#category-label]. If you really need to label a , you can use aria-label or aria-labelledby instead.
+ Found a label with the text of: /Label/, however the element associated with this label () is non-labellable [https://html.spec.whatwg.org/multipage/forms.html#category-label]. If you really need to label a , you can use aria-label or aria-labelledby instead.
-
-
-
-
-
-
-
-
-
-
+
- Hello
-
-
-
-
-
-
-
-
- World
-
-
-
-
-
-
-
-
"
-`)
+
+
+
+
+
+
+
+
+ Hello
+
+
+
+
+
+
+
+
+
+ World
+
+
+
+
+
+
+
+
"
+ `)
})
test('totally empty label', () => {
const {getByLabelText, queryByLabelText} = render(``)
expect(queryByLabelText('')).toBeNull()
expect(() => getByLabelText('')).toThrowErrorMatchingInlineSnapshot(`
-"Found a label with the text of: , however no form control was found associated to that label. Make sure you're using the "for" attribute or "aria-labelledby" attribute correctly.
+ "Found a label with the text of: , however no form control was found associated to that label. Make sure you're using the "for" attribute or "aria-labelledby" attribute correctly.
-
-
-
"
-`)
+
+
+
"
+ `)
})
test('getByLabelText with aria-label', () => {
@@ -1066,7 +1066,7 @@ test('get throws a useful error message without DOM in Cypress', () => {
`"Unable to find an element with the placeholder text of: LucyRicardo"`,
)
expect(() => getByText('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(
- `"Unable to find an element with the text: LucyRicardo. 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."`,
+ `"Unable to find an element using the provided matcher.The matcher is - LucyRicardo 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."`,
)
expect(() => getByTestId('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(
`"Unable to find an element by: [data-testid=\\"LucyRicardo\\"]"`,
@@ -1219,14 +1219,14 @@ test('return a proper error message when no label is found and there is an aria-
expect(() => getByLabelText('LucyRicardo'))
.toThrowErrorMatchingInlineSnapshot(`
-"Unable to find a label with the text of: LucyRicardo
-
-
-
-
"
-`)
+ "Unable to find a label with the text of: LucyRicardo
+
+
+
+
"
+ `)
})
// https://github.com/testing-library/dom-testing-library/issues/723
diff --git a/src/__tests__/queries.find.js b/src/__tests__/queries.find.js
index 6372f957..4db82a3c 100644
--- a/src/__tests__/queries.find.js
+++ b/src/__tests__/queries.find.js
@@ -115,7 +115,13 @@ test('find rejects when something cannot be found', async () => {
await expect(findAllByPlaceholderText('x', qo, wo)).rejects.toThrow('x')
await expect(findByText('x', qo, wo)).rejects.toThrow('x')
+ await expect(findByText(() => {}, qo, wo)).rejects.toThrow(
+ 'Unable to find an element using the provided matcher',
+ )
await expect(findAllByText('x', qo, wo)).rejects.toThrow('x')
+ await expect(findAllByText(() => {}, qo, wo)).rejects.toThrow(
+ 'Unable to find an element using the provided matcher',
+ )
await expect(findByAltText('x', qo, wo)).rejects.toThrow('x')
await expect(findAllByAltText('x', qo, wo)).rejects.toThrow('x')
diff --git a/src/queries/text.js b/src/queries/text.js
index 903bba25..f38bcf8f 100644
--- a/src/queries/text.js
+++ b/src/queries/text.js
@@ -35,8 +35,14 @@ function queryAllByText(
const getMultipleError = (c, text) =>
`Found multiple elements with the text: ${text}`
-const getMissingError = (c, text) =>
- `Unable to find an element with the text: ${text}. 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.`
+const getMissingError = (c, text) => {
+ const errorMsgBase = `Unable to find an element using the provided matcher.`
+ let errorMsgExtnd = ''
+ if (typeof text !== 'function') {
+ errorMsgExtnd = `The matcher is - ${text} 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.`
+ }
+ return errorMsgBase.concat(errorMsgExtnd)
+}
const queryAllByTextWithSuggestions = wrapAllByQueryWithSuggestion(
queryAllByText,