Saltar al contenido principal

ByText

[Traducción Beta No Oficial]

Esta página fue traducida por PageTurner AI (beta). No está respaldada oficialmente por el proyecto. ¿Encontraste un error? Reportar problema →

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

Buscará todos los elementos que contengan un nodo de texto con textContent que coincida con el TextMatch especificado.

<a href="/about">About ℹ️</a>
import {screen} from '@testing-library/dom'

const aboutAnchorNode = screen.getByText(/about/i)

También funciona con inputs cuyo atributo type sea submit o button:

<input type="submit" value="Send data" />

Opciones

Opciones de TextMatch, más las siguientes:

selector

Nota

Consulta getByLabelText para más detalles sobre cómo y cuándo usar la opción selector

ignore

La opción ignore acepta un selector de consulta. Si node.matches devuelve true para ese selector, se ignorará el nodo. Por defecto es 'script, style' porque generalmente no quieres seleccionar estas etiquetas, pero si tu contenido está en un archivo de script en línea, la etiqueta script podría devolverse.

Si prefieres deshabilitar este comportamiento, establece ignore como false.