Skip to content

Adds matcher name to multiple elements error #677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

jacobparis
Copy link
Collaborator

@jacobparis jacobparis commented Jun 28, 2020

What:
The getByRole queries are designed to return a single element whose role matches the provided role argument and whose accessible name matches the provided name argument.

When multiple elements are matched, the language in the error message only mentions the role and not the accessible name.

Sample query

screen.getByRole('definition', { name: /value/i })

Current error message

TestingLibraryElementError: Found multiple elements with the role "definition"

New error messages

TestingLibraryElementError: Found multiple elements with the role "definition" and name "value"
TestingLibraryElementError: Found multiple elements with the role "definition" and name `/value/i`

Why:

When you have several queries against the same role but for different accessible names, the error messages produced by them are identical. The only way to figure out which name is finding duplicates is to look at the code trace.

How:

The information including the accessible name parameter was already provided to the error message handler. The logic for displaying the name with or without quotes depending on its type was already present in the getMissingError handler, so I copied it to the getMultipleError handler

Checklist:

  • Documentation added to the
    docs site N/A
  • Tests
  • Typescript definitions updated N/A
  • Ready to be merged

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jun 28, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 1febc64:

Sandbox Source
icy-breeze-hc34r Configuration

@kentcdodds
Copy link
Member

Love it. Thanks! Could you add a test for this as well?

@jacobparis
Copy link
Collaborator Author

There did not appear to be a test explicitly covering the multiple element error in the first place so I've added a new one

@kentcdodds
Copy link
Member

Looks like we're not quite there on coverage. Let me know if you need help identifying what's left.

@jacobparis
Copy link
Collaborator Author

Just pushed up the missing test

@codecov
Copy link

codecov bot commented Jun 28, 2020

Codecov Report

Merging #677 into master will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #677   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           24        24           
  Lines          596       602    +6     
  Branches       148       151    +3     
=========================================
+ Hits           596       602    +6     
Impacted Files Coverage Δ
src/queries/role.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 373dbc4...1febc64. Read the comment docs.

Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool 👍

@kentcdodds kentcdodds merged commit aefebfb into testing-library:master Jun 28, 2020
@kentcdodds
Copy link
Member

@all-contributors please add @jacobparis for code and tests

@allcontributors
Copy link
Contributor

@kentcdodds

I've put up a pull request to add @jacobparis! 🎉

@kentcdodds
Copy link
Member

🎉 This PR is included in version 7.18.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants