Skip to content

Commit fc30570

Browse files
committed
add guidance on not using implicit assertion for explicit assertions
1 parent c54bd20 commit fc30570

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

docs/testing/cypress.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,38 @@ The following shows an example:
222222
cy.get('#foo')
223223
// Once #foo is found the following:
224224
.contains('Submit')
225+
.click()
225226
// ^ will continue to search for something that has text `Submit` and fail if it times out.
227+
// ^ After it is found trigger a click on the HTML Node that contained the text `Submit`.
228+
```
229+
## Tip: Implicit assertion
230+
Cypress has a concept of implicit assertion. These only kick in if a future command is erroring because of a previous command. E.g. The following will not error if there is no text with `Submit`:
231+
232+
```ts
233+
cy.get('#foo')
234+
// Once #foo is found the following:
235+
.contains('Submit')
236+
```
237+
However the following will error at `contains` (after automatic retries of course) as nothing found can get `click`ed:
238+
239+
```ts
240+
cy.get('#foo')
241+
// Once #foo is found the following:
242+
.contains('Submit')
226243
.click()
227-
// ^ will trigger a click on the HTML Node that contained the text `Submit`.
244+
// ^ Error: #foo does not have anything that `contains` `'Submit'`
245+
```
246+
247+
If you want to assert *use an explicit assertion* and don't rely on implicit assertions. e.g. instead of `contains` you would `cy.should('contain','Submit')` e.g.
248+
249+
```ts
250+
// Bad. No error.
251+
cy.get('#foo')
252+
.contains('Submit')
253+
254+
// Good. Error: `#foo` does not contain `Submit`
255+
cy.get('#foo')
256+
.should('contain', 'Submit')
228257
```
229258

230259
## Tip: Waiting for an HTTP request

0 commit comments

Comments
 (0)