mirror of
https://github.com/cypress-io/cypress.git
synced 2026-05-18 22:28:38 -05:00
more assertions info
This commit is contained in:
@@ -156,7 +156,13 @@ The Cypress Command driver also does things one after the other. Each `cy.` meth
|
||||
|
||||
So a test function made up entirely of Cypress commands will itself execute quite fast: all it really does is enqueue a bunch of actions. Once your function exits, Cypress is ready to kick off and start executing those actions, first-in-first-out. One. Action. At. A. Time.
|
||||
|
||||
# To Assert, or Not To Assert?
|
||||
# Assertions
|
||||
|
||||
Cypress bundles popular assertion libraries for you, and also uses them internally, automatically. It exposes synchronous and asynchronous assertion interfaces, so you're always a few keystrokes away from an expressive test.
|
||||
|
||||
But before we talk about _how_ to assert, let's talk about _whether_ to assert!
|
||||
|
||||
## To Assert, or Not To Assert?
|
||||
|
||||
Cypress makes dozens of assertions available to you via its included libraries, but sometimes the best test may make no assertions at all! What do we mean by this? Let's look at an example:
|
||||
|
||||
@@ -186,3 +192,62 @@ Can you think of any more?
|
||||
Cypress expects this veritable minefield of modern web development and seeks to visualize all this chaos in a reasonable way. Failures are important! Cypress makes them obvious and easy to understand.
|
||||
|
||||
As such, it may be beneficial to relax your test-obsessed mind and take a leisurely drive through your application: visit some pages, click some links, type into some fields, and call it a day. You can rest assured that _so many things **must** be working_ in order for you to be able to navigate from Page A to Page Z without error. If anything is fishy, Cypress will tell you about it... with laser focus.
|
||||
|
||||
## Writing an Assertion
|
||||
|
||||
There are two ways to write assertions in Cypress.
|
||||
|
||||
1. **Implicit Subjects:** Using [`cy.should`](https://on.cypress.io/api/should) or [`cy.and`](https://on.cypress.io/api/and)
|
||||
2. **Explicit Subjects:** Using `expect`
|
||||
|
||||
## Implicit Subjects with [`cy.should`](https://on.cypress.io/api/should) or [`cy.and`](https://on.cypress.io/api/and)
|
||||
|
||||
Using [`cy.should`](https://on.cypress.io/api/should) or [`cy.and`](https://on.cypress.io/api/and) commands is the preferred way of making an assertion in Cypress. These are typical Cypress Commands, which means they can be called against a Chainer and will ultimately be applied to the current Subject flowing through the chain.
|
||||
|
||||
```javascript
|
||||
// the implicit subject here is the first <tr>
|
||||
// this asserts that the <tr> has an .active class
|
||||
cy.get("tbody tr:first").should("have.class", "active")
|
||||
```
|
||||
|
||||

|
||||
|
||||
## Explicit Subjects with `expect`
|
||||
|
||||
Using `expect` allows you to pass in a specific subject and make an assertion on the specified subject. These assertions are more commonly used when writing unit tests, but can also be used when writing integration tests.
|
||||
|
||||
```js
|
||||
// the explicit subject here is the boolean: true
|
||||
expect(true).to.be.true
|
||||
```
|
||||
|
||||
{% note info Unit Testing %}
|
||||
Check out our example recipes for [unit testing](https://github.com/cypress-io/cypress-example-recipes/blob/master/cypress/integration/unit_test_application_code_spec.js) and [unit testing React components](https://github.com/cypress-io/cypress-example-recipes/blob/master/cypress/integration/unit_test_react_enzyme_spec.js)
|
||||
{% endnote %}
|
||||
|
||||
Explicit assertions are great when you want to perform custom logic prior to making the assertion. The usual caveats apply if you want to do work against the Subject: you'll need to do it asynchronously! The `.should()` Command allows us to pass a function that will be yielded the Subject, much the way `.then()` works.
|
||||
|
||||
```javascript
|
||||
cy
|
||||
.get("p")
|
||||
.should(function($p){
|
||||
// return an array of texts from all of the p's
|
||||
var texts = $p.map(function(i, el){
|
||||
return cy.$(el).text()
|
||||
})
|
||||
|
||||
// jquery map returns jquery object
|
||||
// and .get() convert this to simple array
|
||||
var texts = texts.get()
|
||||
|
||||
// array should have length of 3
|
||||
expect(texts).to.have.length(3)
|
||||
|
||||
// set this specific subject
|
||||
expect(texts).to.deep.eq([
|
||||
"Some text from first p",
|
||||
"More text from second p",
|
||||
"And even more text from third p"
|
||||
])
|
||||
})
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user