Skip to content

feat: Enhance ByText error with selector #1159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/__tests__/element-queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ test('get throws a useful error message', () => {
<div />
</div>
`)
expect(() => getByText('LucyRicardo', {selector: 'span'}))
.toThrowErrorMatchingInlineSnapshot(`
Unable to find an element with the text: LucyRicardo, which matches selector 'span'. 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.

Ignored nodes: comments, script, style
<div>
<div />
</div>
`)
expect(() => getByTestId('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(`
Unable to find an element by: [data-testid="LucyRicardo"]

Expand Down Expand Up @@ -1297,7 +1306,7 @@ test('ByText error message ignores not the same elements as configured in `ignor
expect(() =>
getByText('.css-selector', {selector: 'style', ignore: 'script'}),
).toThrowErrorMatchingInlineSnapshot(`
Unable to find an element with the text: .css-selector. 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 with the text: .css-selector, which matches selector 'style'. 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.

Ignored nodes: comments, script, style
<body>
Expand Down
5 changes: 4 additions & 1 deletion src/queries/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,17 @@ const getMissingError: GetErrorFunction<[Matcher, SelectorMatcherOptions]> = (
text,
options = {},
) => {
const {collapseWhitespace, trim, normalizer} = options
const {collapseWhitespace, trim, normalizer, selector} = options
const matchNormalizer = makeNormalizer({collapseWhitespace, trim, normalizer})
const normalizedText = matchNormalizer(text.toString())
const isNormalizedDifferent = normalizedText !== text.toString()
const isCustomSelector = (selector ?? '*') !== '*'
return `Unable to find an element with the text: ${
isNormalizedDifferent
? `${normalizedText} (normalized from '${text}')`
: text
}${
isCustomSelector ? `, which matches selector '${selector}'` : ''
}. 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.`
}

Expand Down