Skip to content

.within() now throws an error if given more than one subject #4898

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

Merged
merged 3 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions content/_changelogs/12.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ _Released MM/DD/YYYY_
- Cypress now throws an error if commands are invoked from inside a `.should()`
callback. This previously resulted in unusual and undefined behavior; it is
now explicitly an error.
- The [`.within()`](/api/commands/within) command now throws an error if given
more than one DOM element as a subject. This is done for consistency - in
older versions, some commands inside a `.within()` block would respect all
passed in elements, while others silently discarded subjects beyond the first
and `.screenshot()` would throw an error.
- `Cookies.defaults` and `Cookies.preserveOnce` have been removed. Please update
to use [`cy.session()`](/api/commands/session) to preserve session details
between tests. Addresses
Expand Down
17 changes: 11 additions & 6 deletions content/api/commands/within.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ chain further commands that rely on the subject after `.within()`.
**<Icon name="check-circle" color="green"></Icon> Correct Usage**

```javascript
cy.get('.list').within(($list) => {}) // Yield the `.list` and scope all commands within it
cy.get('.list')
.first()
.within(($list) => {}) // Yield the first `.list` and scope all commands within it
```

**<Icon name="exclamation-triangle" color="red"></Icon> Incorrect Usage**

```javascript
cy.within(() => {}) // Errors, cannot be chained off 'cy'
cy.getCookies().within(() => {}) // Errors, 'getCookies' does not yield DOM element
cy.get('div').within(($divs) => {}) // Probably errors, because get('div') yields multiple elements
```

### Arguments
Expand Down Expand Up @@ -179,7 +182,8 @@ cy.get('form').within(($form) => {

### Requirements [<Icon name="question-circle"/>](/guides/core-concepts/introduction-to-cypress#Chains-of-Commands)

<List><li>`.within()` requires being chained off a previous command.</li></List>
- `.within()` requires being chained off a previous command that yields exactly
one DOM element.

### Assertions [<Icon name="question-circle"/>](/guides/core-concepts/introduction-to-cypress#Assertions)

Expand Down Expand Up @@ -211,10 +215,11 @@ outputs the following:

## History

| Version | Changes |
| --------------------------------------------- | ------------------------------------------------------- |
| [< 0.3.3](/guides/references/changelog#0-3-3) | `.within()` command added |
| [5.4.0](/guides/references/changelog#5-4-0) | fixed the yielded value to always be the parent element |
| Version | Changes |
| --------------------------------------------- | -------------------------------------------------------------------------------- |
| [12.0.0](/guides/references/changelog#12-0-0) | `.within()` now throws an error when given more than one element as the subject. |
| [5.4.0](/guides/references/changelog#5-4-0) | fixed the yielded value to always be the parent element |
| [< 0.3.3](/guides/references/changelog#0-3-3) | `.within()` command added |

## See also

Expand Down
74 changes: 70 additions & 4 deletions content/guides/references/migration-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,10 @@ command instead.

#### `.invoke()`

[`.invoke()`](/api/commands/invoke) now throws an error if the function returns
a promise. If you wish to call a method that returns a promise and wait for it
to resolve, use [`.then()`](/api/commands/then) instead of `.invoke()`.
The [`.invoke()`](/api/commands/invoke) command now throws an error if the
function returns a promise. If you wish to call a method that returns a promise
and wait for it to resolve, use [`.then()`](/api/commands/then) instead of
`.invoke()`.

```diff
cy.wrap(myAPI)
Expand All @@ -296,7 +297,72 @@ assertions to their own chain. For example, rewrite

#### `.should()`

[`.should()`](/api/commands/should) now throws an error if Cypress commands are
The [`.should()`](/api/commands/should) assertion now throws an error if Cypress
commands are invoked from inside a `.should()` callback. This previously
resulted in unusual and undefined behavior. If you wish to execute a series of
commands on the yielded value, use`.then()` instead.

```diff
cy.get('button')
- .should(($button) => {

})
+ .then(api => api.makeARequest('http://example.com'))
.then(res => { ...handle response... })
```

#### `.within()`

The [`.within()`](/api/commands/within) command now throws an error if it is
passed multiple elements as the subject. This previously resulted in
inconsistent behavior, where some commands would use all passed in elements,
some would use only the first and ignore the rest, and
[`.screenshot()`](/api/commands/screenshot) would throw an error if used inside
a `.within()` block with multiple elements.

If you were relying on the old behavior, you have several options depending on
the desired result.

The simplest option is to reduce the subject to a single element.

```diff
cy.get('tr')
+ .first() // Limit the subject to a single element before calling .within()
.within(() => {
cy.contains('Edit').click()
})
```

If you have multiple subjects and wish to run commands over the collection as a
whole, you can alias the subject rather than use `.within()`.

```diff
cy.get('tr')
- .within(() => {
- cy.get('td').should('have.class', 'foo')
- cy.get('td').should('have.class', 'bar')
- })
+ .as('rows') // Store multiple elements as an alias

+cy.get('@rows').find('td').should('have.class', 'foo')
+cy.get('@rows').find('td').should('have.class', 'bar')
```

Or if you have a collection and want to run commands over every element, use
`.each()` in conjunction with `.within()`.

```diff
cy.get('tr')
- .within(() => {
- cy.contains('Edit').should('have.attr', 'disabled')
- })
+ .each($tr => {
+ cy.wrap($tr).within(() => {
+ cy.contains('Edit').should('have.attr', 'disabled')
+ })
+ })
```

invoked from inside a `.should()` callback. This previously resulted in unusual
and undefined behavior. If you wish to execute a series of commands on the
yielded value, use`.then()` instead.
Expand Down