ByText
getByText, queryByText, getAllByText, queryAllByText, findByText, findAllByText
APIâ
getByText(
// If you're using `screen`, then skip the container argument:
container: HTMLElement,
text: TextMatch,
options?: {
selector?: string = '*',
exact?: boolean = true,
ignore?: string|boolean = 'script, style',
normalizer?: NormalizerFn,
}): HTMLElement
This will search for all elements that have a text node with textContent
matching the given TextMatch
.
<a href="/about">About âšī¸</a>
- Native
- React
- Cypress
import {screen} from '@testing-library/dom'
const aboutAnchorNode = screen.getByText(/about/i)
import {render, screen} from '@testing-library/react'
render(<MyComponent />)
const aboutAnchorNode = screen.getByText(/about/i)
cy.findByText(/about/i).should('exist')
It also works with input
s whose type
attribute is either submit
or
button
:
<input type="submit" value="Send data" />
Optionsâ
TextMatch options, plus the following:
selector
â
Note
See
getByLabelText
for more details on how and when to use theselector
option
ignore
â
The ignore
option accepts a query selector. If the
node.matches
returns true for that selector, the node will be ignored. This defaults to
'script, style'
because generally you don't want to select these tags, but if
your content is in an inline script file, then the script tag could be returned.
If you'd rather disable this behavior, set ignore
to false
.