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.

Extra space added around embedded expressions in JSX when querying by role + name

See original GitHub issue
  • @testing-library/dom version: 7.5.9
  • Testing Framework and version: jest 25.5.4
  • DOM Environment: jsdom

Relevant code or config:

export default ({ name }) => <h1>Hello {name}!</h1>;
screen.getByRole("heading", { name: /Hello Jill!/i })

What you did:

Tried to query an element by role + name

What happened:

The existing element cannot be found. If the name is in JSX with an embedded expression, potentially inexistent spaces are added around it:

Unable to find an accessible element with the role "heading" and name `/Hello Jill!/i`

Here are the accessible roles:

  heading:

  Name "Hello Jill !":
  <h1 />

Note the extra space here:

 Name "Hello Jill !":
                 ^

Reproduction:

https://codesandbox.io/s/react-testing-library-demo-09wqi

Problem description:

I cannot create accurate queries in those cases (embedded expressions without surrounding space)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
CarlosGinescommented, Jun 5, 2020

Awesome! Such a quick reply and fix! Thank you folks! I did check the CodeSandbox of the PR and it does work as expected ^__^

0reactions
kentcdoddscommented, Jun 5, 2020

🎉 This issue has been resolved in version 7.10.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Read more comments on GitHub >

github_iconTop Results From Across the Web

Best practice when adding whitespace in JSX - Stack Overflow
To add a space via JSX, simply put a string consisting of one space character in a JSX expression. The simplest way to...
Read more >
Render a String with Non-breaking Spaces in React | Pluralsight
If your JSX element is such that the white spaces are equivalent to empty spaces in the string, you can simply set this...
Read more >
What are inline conditional expressions in ReactJS
In this tutorial, we will go through the concept of inline conditional rendering in React. As the inline condition suggests, we can write...
Read more >
Understanding Template Literals in JavaScript - DigitalOcean
With template literals, an expression can be embedded in a placeholder. A placeholder is represented by ${} , with anything within the curly ......
Read more >
JSX in React – Explained with Examples - freeCodeCamp
JSX Expressions, which are written inside curly brackets, allow only things that evaluate to some value like string, number, array map method ...
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