question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support for <br /> (line breaks) when matching text

See original GitHub issue

Describe the feature you’d like:

Given the following markup:

<div>Hello<br />World</div>

considering that innerText of the above element would be "Hello\nWorld" (see this JSFiddle), I expect that I would be able to use this query:

screen.getByText("Hello\nWorld");

but it throws the following error:

TestingLibraryElementError: Unable to find an element with the text: Hello
World. 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.

This example on CodeSandbox

Suggested implementation:

Describe alternatives you’ve considered:

I’ve considered using a custom function, like the error message suggests, but this is my first experience with Testing Library, and I don’t really know how to do it yet.

And since it’s a simple case, I would rather like to use a simple query, without providing any custom functions.

Teachability, Documentation, Adoption, Migration Strategy:

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
kentcdoddscommented, Aug 26, 2020

That makes sense. One thing you could do is this:

expect(screen.getByText(/look for an email/i)).toMatchInlineSnapshot()

That would output everything with the <br />. I’m just not sure how I feel about encoding things like this in the query because you could implement that same thing using spans/divs/etc.

3reactions
christo8989commented, Feb 26, 2022

@EvgenyOrekhov

Might not be the best solution but it’s working for me.

screen.getByText(/Look for an email from.*if you don't see it, check your spam folder./);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Break the line in a concatenated string based on a matching ...
Using "br". You can simply append the text using innerHTML like: var content = "Test 1<br>Test 2<br>Test 3" document.
Read more >
<br>: The Line Break element - HTML - MDN Web Docs - Mozilla
The HTML element produces a line break in text (carriage-return). It is useful for writing a poem or an address, where the division...
Read more >
Insert a line break in a cell - Microsoft Support
To add space between lines of text in a cell, double-click the cell, click where you want to break the line, and then...
Read more >
The Dos and Don'ts of Adding an HTML Line Break
In HTML, the <br> element creates a line break. You can add it wherever you want text to end on the current line...
Read more >
Using the \n formula to add line breaks in Airtable
When working with formulas, especially when combining several fields of information, it is often helpful to program in line breaks and other ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found