Implicit Assertions


.should()

To make an assertion about the current subject, use the .should() command.

cy.get('.assertion-table')
  .find('tbody tr:last').should('have.class', 'success')
# Column heading Column heading
1 Column content Column content
2 Column content Column content
3 Column content Column content

.and()

To chain multiple assertions together, use the .and() command.

cy.get('.assertions-link')
  .should('have.class', 'active')
  .and('have.attr', 'href')
  .and('include', 'cypress.io')

Explicit Assertions


expect

To make a BDD assertion about a specified subject, use expect.

// We can use Chai's BDD style assertions
expect(true).to.be.true

// Pass a function to should that can have any number
// of explicit assertions within it.
cy.get('.assertions-p').find('p')
.should(($p) => {
  // return an array of texts from all of the p's
  let texts = $p.map((i, el) => // https://on.cypress.io/$
    Cypress.$(el).text())

  // jquery map returns jquery object
  // and .get() convert this to simple array
  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',
  ])
})

Some text from first p

More text from second p

And even more text from third p


assert

To make a TDD assertion about a specified subject, use assert.

const person = {
  name: 'Joe',
  age: 20,
}
assert.isObject(person, 'value is object')