Skip to content
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

findBy* retry-ability when multiple elements found #83

Closed
wants to merge 3 commits into from

Conversation

Jmaharman
Copy link
Contributor

What:

Add retry-ability to findBy* when multiple elements are matched.

Why:

findBy* queries would fail early if they match multiple elements, when it could automatically retry.

*How:

This catches the multiple matches exception, and retries until the request times out.

Checklist:

[ ] Documentation
[x] Tests
[x] Ready to be merged

Side Effects

Unfortunately this change comes with side effects, in that the error message when this scenario occurs says that the element cannot be found. See the last commit for the new error message being expected in the test.

I can't see a way around that for now, and as such I'm not sure if we should include this change.

I thought I'd commit this PR so that others could see it, and consider another way of doing it.

Only relevant for the findBy* queries, due to it throwing an error if multiple elements are found.
This update changed the failure message for matching multiple elements.

Unfortunately I can't see away around it if we are using retry-ability.
// Only catch exceptions for multiple elements found, which we will continue to retry
if (/Found multiple elements/.test(err.message)) {
// Cannot delete after, because consoleProps() is called on the log object before I get the chance
consoleProps.CaughtError = err;
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you (conditionally) clear the value in the try block?

@@ -118,12 +123,12 @@ describe('find* dom-testing-library commands', () => {
})

it('findByText finding multiple items should error', () => {
const errorMessage = `Found multiple elements with the text: /^Button Text/i\n\n(If this is intentional, then use the \`*AllBy*\` variant of the query (like \`queryAllByText\`, \`getAllByText\`, or \`findAllByText\`)).`
const errorMessage = `Timed out retrying: Expected to find element: 'findByText(/^Button Text/i)', but never found it.`
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah this is kind of confusing... Let's hold while we figure it out

@kentcdodds
Copy link
Member

Hi folks 👋 @Jmaharman, do you still have plans to work on this or should we have someone else pick up where you left off? No pressure, just wondering how to proceed :)

NicholasBoll added a commit to NicholasBoll/cypress-testing-library that referenced this pull request Jan 30, 2020
* Add element selector infomation for debugging (outlines element when you click on command) (fixes testing-library#103)
* Add @testing-library/dom errors (from `get*` queries) to failure messages - these are more helpful than the generic `find*('input') does not exist` messages (fixes testing-library#103)
* Add retryability to `findBy*` when multiple elements are found (fixes testing-library#83)
* Add option to disable logging of all commands
* `query*` and `find*` have a consistent code path and error messaging (fixes testing-library#103)
* Remove usage of Cypress commands in queries (fixes testing-library#103)
NicholasBoll added a commit to NicholasBoll/cypress-testing-library that referenced this pull request Jan 30, 2020
* Add element selector information for debugging (outlines element when you click on command) (fixes testing-library#103)
* Add @testing-library/dom errors (from `get*` queries) to failure messages - these are more helpful than the generic `find*('input') does not exist` messages (fixes testing-library#103)
* Add retryability to `findBy*` when multiple elements are found (fixes testing-library#83)
* Add option to disable logging of all commands
* `query*` and `find*` have a consistent code path and error messaging (fixes testing-library#103)
* Remove usage of Cypress commands in queries (fixes testing-library#103)
@Jmaharman
Copy link
Contributor Author

Jmaharman commented Jan 30, 2020 via email

@kentcdodds
Copy link
Member

No problem @Jmaharman, thanks anyway!

I think this will actually be handled by #108.

@kentcdodds kentcdodds closed this Jan 31, 2020
kentcdodds pushed a commit that referenced this pull request Feb 2, 2020
* feat: add more helpful debugging information to queries
* Add element selector information for debugging (outlines element when you click on command) (fixes #103)
* Add @testing-library/dom errors (from `get*` queries) to failure messages - these are more helpful than the generic `find*('input') does not exist` messages (fixes #103)
* Add retryability to `findBy*` when multiple elements are found (fixes #83)
* Add option to disable logging of all commands
* `query*` and `find*` have a consistent code path and error messaging (fixes #103)
* Remove usage of Cypress commands in queries (fixes #103)

* feat: add ability for queries to inherit previous subject
* Fixes #109 without breaking change caused by #100

* feat: add parent/child log type detection

* chore: implement feedback

* docs: update readme to complete my thought process

* slightly simplify config fn

* Update README.md

Co-authored-by: Kent C. Dodds <me+github@kentcdodds.com>

Closes #103, Closes #109, Closes #110
@kentcdodds
Copy link
Member

🎉 This issue has been resolved in version 5.2.0 🎉

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.

3 participants