fix: .within() now throws an error if given more than one subject.#24975
Conversation
|
Thanks for taking the time to open a PR!
|
| validateSessionsInstrumentPanel(['blank_session']) | ||
|
|
||
| cy.get('.command-name-session') | ||
| .first() |
There was a problem hiding this comment.
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.
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>
marktnoonan
left a comment
There was a problem hiding this comment.
Works well, I get the error when there's more than 1 subject.
chrisbreiding
left a comment
There was a problem hiding this comment.
Looks good! I like the error message.
mschile
left a comment
There was a problem hiding this comment.
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 awithinblock 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?