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.

`getByTitle` for SVGs canot find `title` if it's not a direct child of `svg`

See original GitHub issue
  • @testing-library/react version: 11.2.7
  • Testing Framework and version: jest 26x || 27x
  • DOM Environment: ‘jsdom’ 16.x

Relevant code or config:

Source: https://testing-library.com/docs/queries/bytitle/

    const Test = () => (
        <>
            <span title="Delete" id="2"></span>
            <svg>
                <title>Close</title>
                <g><path /></g>
            </svg>
        </>
    );

VS

    const Test = () => (
        <>
            <span title="Delete" id="2"></span>
            <svg>
                <g>
                    <path>
                         <title>Close</title>
                    </path>
                </g>
            </svg>
       </>
    );

What you did:

render(<Test/>);

screen.getByTitle('Close');

What happened:

First Component - ✅ Second Component - ❌

TestingLibraryElementError: Unable to find an element with the title: Close.

Problem description:

Testing library cannot find Close because it’s not a direct child of svg.

Suggested solution:

I should be able to find Close even if it’s not a direct child of svg.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
eps1loncommented, Dec 7, 2021
1reaction
eps1loncommented, Jun 9, 2021

Just guessing here: getByText("Close", selector: 'title') and then walk up to the nearest SVG container or graphics element.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ByTitle - Testing Library
Returns the element that has the matching title attribute. Will also find a title element within an SVG. <span title ...
Read more >
<title> — the SVG accessible name element - MDN Web Docs
If an element can be described by visible text, it is recommended to reference that text with an aria-labelledby attribute rather than using...
Read more >
Test correct SVG component renders with jest and react ...
The second test uses findByTitle because if it is not in the document, getBy* makes the test fails directly. Note: to get this...
Read more >
Contextually Marking up accessible images and SVGs
Using a title attribute on an image or SVG. ... if not using <use> then the child elements of the inline SVG would...
Read more >
SVG icons - W3C Design System
This section specifically covers using inline SVG files as icons. ... This prevents the SVG from expanding to fill the whole page if...
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