-
Notifications
You must be signed in to change notification settings - Fork 154
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
Support running commands against the previous yielded subject #100
Support running commands against the previous yielded subject #100
Conversation
…lean up similar within/container tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fantastic. Thank you so much!
Would this mean this also works: cy.findByRole('dialog').findByText('submit')
?
That would be stellar. Thanks again!
cy.findByText(/^Button Text/, {container: subject}).click() | ||
}) | ||
// NOTE: Cypress' `then` doesn't actually return a promise | ||
// eslint-disable-next-line jest/valid-expect-in-promise |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably disable the jest plugin for these tests. But we can do that in another PR :)
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry about all these. Don't know how those slipped through in the first place
it('findByText with a previous subject', () => { | ||
cy.get('#nested') | ||
.findByText('Button Text 1') | ||
.should('not.exist') | ||
cy.get('#nested') | ||
.findByText('Button Text 2') | ||
.should('exist') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love this
@all-contributors please add @tlrobinson for code and tests |
I've put up a pull request to add @tlrobinson! 🎉 |
Thanks so much for your help! I've added you as a collaborator on the project. Please make sure that you review the |
🎉 This PR is included in version 5.1.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
FYI this was a breaking change - all my tests that assumed findByText used the document broke after upgrading. It might be worth mentioning this in the releases, or bumping the major version if your doing semver. |
@twgraham out of curiosity, what did your tests that broke look like? This change shouldn’t break queries like |
@tlrobinson kind of like the latter... I suppose we were treating a bit like 'get' because we realised early on that the previous subject wasn't yielded, so we had to do something like I think this PR is definitely a great idea - just didn't want people to get bitten unnecessarily. |
More discussion on this in #109. Please comment when you're able :) |
Great update and definitely prefer this style but as mentioned above, this has introduced a breaking change. Example usage: |
* Fixes testing-library#109 without breaking change caused by testing-library#100
* 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 <[email protected]> Closes #103, Closes #109, Closes #110
What:
Changes the custom Cypress commands to respect the previously yielded subject, for example, with the following document:
this
would previously fail because
findByText
was not passed the result ofcy.get('#nested')
as itscontainer
(defaulting todocument
)Why:
It's a convenient way to scope commands to the result of a previous command, and for users of builtin Cypress commands it's surprising that
@testing-library/cypress
doesn't currently behave like this.How:
The
prevSubject
option is now passed toCypress.Commands.add()
(with a value of['optional', 'document', 'element', 'window']
which matches builtin commands likecontains
. This causes the subject (if any) to be passed as the first argument to the command. The subject is given lower precedence than thetesting-library
'scontainer
option, but higher precedence than the document itself.Checklist: