-
Notifications
You must be signed in to change notification settings - Fork 3.3k
fix: .within() now throws an error if given more than one subject. #24975
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
Thanks for taking the time to open a PR!
|
@@ -106,6 +106,7 @@ describe('runner/cypress sessions.ui.spec', { | |||
validateSessionsInstrumentPanel(['blank_session']) | |||
|
|||
cy.get('.command-name-session') | |||
.first() |
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.
There are a couple of examples where our tests used to work, but required a change because of this PR. For example, this test had multiple commands - we now are explicitly (rather than implicitly) asserting on the first of them.
cy.containsPath('cypress/support/commands.js') | ||
cy.containsPath('cypress/fixtures/example.json') | ||
}) | ||
cy.get('[data-cy=valid]').as('valid').contains('cypress.config.js') |
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.
Here's an example of where we were genuinely relying on multiple subjects from .within()
, and it required something more than just .first()
to fix the test.
Test summaryRun details
View run in Cypress Dashboard ➡️ This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Dashboard |
Co-authored-by: Emily Rohrbough <emilyrohrbough@users.noreply.github.com>
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.
Works well, I get the error when there's more than 1 subject.
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.
Looks good! I like the error message.
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.
Should we have a test that explicitly tests our recommendation of use .each
?
I don't see the value. Each command is tested individually - we know they all work. 🤷♀️ |
User facing changelog
BREAKING:
.within()
now throws an error if given more than one subject.This brings the behavior in line with the documentation, and adds consistency around how
.within()
behaves across commands. Some commands inside awithin
block would silently select the first element, while others would use all of them, and still others would throw an error.This ambiguity is now resolved - all commands use the first subject element, because only one element is allowed.
Additional details
Steps to test
See the new test added in this PR for an example of the change in action.
How has the user experience changed?
A visual example of the error thrown.
PR Tasks
cypress-documentation
? .within() now throws an error if given more than one subject cypress-documentation#4898type definitions
?