testing-library / testing-library/dom-testing-library

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

Open
#974 8 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
JavaScript
Stars
3.3k
Forks
474
PR merge metrics
No merged PRs in 30d

Description

  • @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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the getByTitle query entry point and reproduce the nested SVG example from the issue, comparing it with the direct-child case. Add or update a regression test so screen.getByTitle('Close') succeeds when the title is nested, then run the relevant JavaScript test suite.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.