I am testing react app with react testing library and I came cross this error
Unable to find an element with the text: /9900/i. 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.
I have changed from toBeInTheDocument() to toEqual() but it didn't work too, and I tried to pass a function but it didn't work too?
const getByTextContent = (text) => {
return screen.getByText((content, element) => {
const hasText = element => element.textContent === text
const elementHasText = hasText(element)
const childrenDontHaveText = Array.from(element?.children || []).every(child => !hasText(child))
return elementHasText && childrenDontHaveText
})
}
Here is the test
expect(dataFetcher).toBeCalledTimes(1);
await waitFor(() => {
const playerId = screen.getByText(/9900/i);
expect(playerId.value).toEqual(9900);
expect(playerId).toBeInTheDocument(); // I tried this too but ogt the same result
});