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.

should getByText return a title?

See original GitHub issue
  • @testing-library/dom version: latest master
  • Testing Framework and version: node, jest 12.x, 26.x
  • DOM Environment:

Relevant code or config:

test('foo', () => {
  renderIntoDocument(` <svg>
  <title data-testid="svg">Close</title>
  <g><path /></g>
</svg>`)

  screen.debug(screen.getByText('Close'))
})

What you did:

called screen.getByText

What happened:

I got back the title… but shouldn’t getByText be accessible to everyone? If it’s hidden, it won’t be accessible to sighted users.

Reproduction:

Problem description:

Suggested solution:

have getByText ignore <title> elements.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:12 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
kentcdoddscommented, May 29, 2020

This change of mind brought to you by: https://github.com/testing-library/cypress-testing-library/issues/134

In fact, the original reason for ignore was because getByText was getting me translation text that was served up in the script tag for my app when using cypress 😅 Seems it would make sense to add some other tags now that I think about it.

Anyone wanna do this?

1reaction
benmonrocommented, May 31, 2020

I actually am using it to check for the presence of svg so I do think we should keep it. I’m just saying it should return the element it’s a title for rather than the title element itself. Just like how get by label text will return the input element rather than the label.

Read more comments on GitHub >

github_iconTop Results From Across the Web

About Queries | Testing Library
findBy... : Returns a Promise which resolves when an element is found which matches the given query. The promise is rejected if no...
Read more >
How to expect a getByText to be false in react testing library?
But I would like to test as well that the modal does not exist before the triger event. So I decided to expect...
Read more >
How to use the react-testing-library.fireEvent.click function in ...
test(`User should be able to add a new checkbox item. ... toThrow(); // Validate TOC not open expect(() => getByText('Title')).toThrow(); // Open TOC...
Read more >
React Testing Library Examples
queryByText and getByText to select a node and assert its presence ... Unchecking will return to the original document title fireEvent.click(checkbox); ...
Read more >
React-Testing-Library — Pro tips. Extends expect of ... - Jero
In this post, we will see a set of tips and recommendations about the use of the React ... return ( <form> <label...
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