Skip to content

Commit 2536924

Browse files
author
Kent C. Dodds
authored
feat(getByText): add ignore option which defaults to 'script' (testing-library#109)
1 parent c07657d commit 2536924

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ getByText(
297297
exact?: boolean = true,
298298
collapseWhitespace?: boolean = true,
299299
trim?: boolean = true,
300+
ignore?: string|boolean = 'script'
300301
}): HTMLElement
301302
```
302303

@@ -310,6 +311,14 @@ const aboutAnchorNode = getByText(container, 'about')
310311

311312
> NOTE: see [`getByLabelText`](#getbylabeltext) for more details on how and when to use the `selector` option
312313
314+
The `ignore` option accepts a query selector. If the
315+
[`node.matches`](https://developer.mozilla.org/en-US/docs/Web/API/Element/matches)
316+
returns true for that selector, the node will be ignored. This defaults to
317+
`'script'` because generally you don't want to select script tags, but if your
318+
content is in an inline script file, then the script tag could be returned.
319+
320+
If you'd rather disable this behavior, set `ignore` to `false`.
321+
313322
### `getByAltText`
314323

315324
```typescript

src/__tests__/element-queries.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,4 +512,12 @@ test('get throws a useful error message without DOM in Cypress', () => {
512512
expect(() => getByValue('LucyRicardo')).toThrowErrorMatchingSnapshot()
513513
})
514514

515+
test('getByText ignores script tags by default', () => {
516+
const {getAllByText} = render('<script>Hello</script><div>Hello</div>')
517+
const divOnly = getAllByText(/hello/i)
518+
expect(divOnly).toHaveLength(1)
519+
expect(divOnly[0].tagName).toBe('DIV')
520+
expect(getAllByText(/hello/i, {ignore: false})).toHaveLength(2)
521+
})
522+
515523
/* eslint jsx-a11y/label-has-for:0 */

src/queries.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,19 @@ function queryByLabelText(...args) {
6969
function queryAllByText(
7070
container,
7171
text,
72-
{selector = '*', exact = true, collapseWhitespace = true, trim = true} = {},
72+
{
73+
selector = '*',
74+
exact = true,
75+
collapseWhitespace = true,
76+
trim = true,
77+
ignore = 'script',
78+
} = {},
7379
) {
7480
const matcher = exact ? matches : fuzzyMatches
7581
const matchOpts = {collapseWhitespace, trim}
76-
return Array.from(container.querySelectorAll(selector)).filter(node =>
77-
matcher(getNodeText(node), node, text, matchOpts),
78-
)
82+
return Array.from(container.querySelectorAll(selector))
83+
.filter(node => !ignore || !node.matches(ignore))
84+
.filter(node => matcher(getNodeText(node), node, text, matchOpts))
7985
}
8086

8187
function queryByText(...args) {

0 commit comments

Comments
 (0)