I'm trying to match a phone number string that includes a non-breaking space:
assert
.dom(
screen.getByText(
[my text with a non-breaking space]
) as HTMLElement
)
.exists();
However, it is returning this error:
Unable to find an element with the text: [my text with a non-breaking space]. 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.
How can I test this?
The testing library automatically normalizes whitespace, so the non-breaking space gets converted to a regular space by default. See more about the default behavior at: https://testing-library.com/docs/queries/about/#normalization
To override this behavior and leave it as a non-breaking space (so that it will match your
assert
), set thecollapseWhitespace
parameter tofalse
.This will look something like this: