Skip to content

fix: Explain error thrown when cypress commands in .should() callback #4755

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 8 commits into from
Oct 11, 2022
38 changes: 34 additions & 4 deletions content/api/commands/should.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,35 @@ Be sure _not_ to include any code that has side effects in your callback
function. The callback function will be retried over and over again until no
assertions within it throw.

You cannot invoke Cypress commands inside of a `.should()` callback function.
Use Cypress commands before or after `.should()` instead.

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

```javascript
cy.get('p').should(($p) => {
cy.log($p)
// ...
})
```

**<Icon name="check-circle" color="green"></Icon> Correct Usage**

```javascript
cy.get('p')
.should(($p) => {
// ...
})
.log()

// or

cy.get('p').then(($p) => {
// ...
cy.log($p)
})
```

#### Verify length, content, and classes from multiple `<p>`

```html
Expand Down Expand Up @@ -467,10 +496,11 @@ following:

## History

| Version | Changes |
| --------------------------------------------- | --------------------------------- |
| [0.11.4](/guides/references/changelog#0-11-4) | Allows callback function argument |
| [< 0.3.3](/guides/references/changelog#0-3-3) | `.should()` command added |
| Version | Changes |
| --------------------------------------------- | ----------------------------------------------- |
| [11.0.0](/guides/references/changelog#11-0-0) | Throw error if Cypress command used in callback |
| [0.11.4](/guides/references/changelog#0-11-4) | Allows callback function argument |
| [< 0.3.3](/guides/references/changelog#0-3-3) | `.should()` command added |

## See also

Expand Down