React Testing Library .toHaveTextContent exact match

Viewed 4387

How do I make expect(stuff).toHaveTextContent(text) pass only when the text matches exactly?

.toHaveTextContent("1") counts as a match if the text I'm testing is "100"

4 Answers

You can do the ff instead:

expect(stuff.textContent).toBe('100')

Get the the text using innerHTML

const element = await waitFor(() => document.querySelector('<YOUR_QUERY>'));

expect(element.innerHTML).toEqual('100')

Use getByText like so,

const { getByText } = render(<YourComponent />)

expect(getByText('text you\'re looking for')).beInTheDocument()

This assumes it will be there, so if there's a chance it won't be, use queryByText.

Related