Skip to content

Commit d686c92

Browse files
BlueWindsRachelflotwig
authored
fix: Explain error thrown when cypress commands in .should() callback (#4755)
* fix: Explain error thrown when cypress commands in .should() callback * Improve layout of previous changes and provide second example of how to fix * Update content/api/commands/should.md Co-authored-by: Rachel <[email protected]> * Apply suggestions from code review Co-authored-by: Zach Bloomquist <[email protected]> * Run prettier * Run prettier again...? * One more prettier run... :/ Co-authored-by: Rachel <[email protected]> Co-authored-by: Zach Bloomquist <[email protected]>
1 parent 995bf3d commit d686c92

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

content/api/commands/should.md

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,35 @@ Be sure _not_ to include any code that has side effects in your callback
172172
function. The callback function will be retried over and over again until no
173173
assertions within it throw.
174174

175+
You cannot invoke Cypress commands inside of a `.should()` callback function.
176+
Use Cypress commands before or after `.should()` instead.
177+
178+
**<Icon name="exclamation-triangle" color="red"></Icon> Incorrect Usage**
179+
180+
```javascript
181+
cy.get('p').should(($p) => {
182+
cy.log($p)
183+
// ...
184+
})
185+
```
186+
187+
**<Icon name="check-circle" color="green"></Icon> Correct Usage**
188+
189+
```javascript
190+
cy.get('p')
191+
.should(($p) => {
192+
// ...
193+
})
194+
.log()
195+
196+
// or
197+
198+
cy.get('p').then(($p) => {
199+
// ...
200+
cy.log($p)
201+
})
202+
```
203+
175204
#### Verify length, content, and classes from multiple `<p>`
176205

177206
```html
@@ -467,10 +496,11 @@ following:
467496

468497
## History
469498

470-
| Version | Changes |
471-
| --------------------------------------------- | --------------------------------- |
472-
| [0.11.4](/guides/references/changelog#0-11-4) | Allows callback function argument |
473-
| [< 0.3.3](/guides/references/changelog#0-3-3) | `.should()` command added |
499+
| Version | Changes |
500+
| --------------------------------------------- | ----------------------------------------------- |
501+
| [11.0.0](/guides/references/changelog#11-0-0) | Throw error if Cypress command used in callback |
502+
| [0.11.4](/guides/references/changelog#0-11-4) | Allows callback function argument |
503+
| [< 0.3.3](/guides/references/changelog#0-3-3) | `.should()` command added |
474504

475505
## See also
476506

0 commit comments

Comments
 (0)