Cypress._

To call a lodash method, use the Cypress._.method() command.

cy.request('https://jsonplaceholder.cypress.io/users')
.then((response) => {
  let ids = Cypress._.chain(response.body).map('id').take(3).value()

  expect(ids).to.deep.eq([1, 2, 3])
})

Cypress.$

To call a jQuery method, use the Cypress.$ command.

let $li = Cypress.$('.utility-jquery li:first')

cy.wrap($li)
  .should('not.have.class', 'active')
  .click()
  .should('have.class', 'active')
  • 5 Watches
  • 14 Sweaters
  • 22 Scarves

Cypress.Blob

To work with blobs, convert strings, and other utility functions, use the Cypress.Blob library.

cy.get('.utility-blob').then(($div) =>
// https://github.com/nolanlawson/blob-util#imgSrcToDataURL
// get the dataUrl string for the javascript-logo
  Cypress.Blob.imgSrcToDataURL('/assets/img/javascript-logo.png', undefined, 'anonymous')
  .then((dataUrl) => {
    // create an  element and set its src to the dataUrl
    let img = Cypress.$('', { src: dataUrl })
    // need to explicitly return cy here since we are initially returning
    // the Cypress.Blob.imgSrcToDataURL promise to our test
    // append the image
    $div.append(img)

    cy.get('.utility-blob img').click()
      .should('have.attr', 'src', dataUrl)
  }))

Cypress.minimatch

To test out glob patterns against strings, use the Cypress.minimatch library.

Cypress.minimatch('/users/1/comments', '/users/*/comments', {
  matchBase: true,
})

Cypress.moment()

To parse or format a date using a moment method, use the Cypress.moment() command.

const time = Cypress.moment().utc('2014-04-25T19:38:53.196Z').format('h:mm A')

cy.get('.utility-moment').contains('3:38 PM')
  .should('have.class', 'badge')

// the time in the element should be between 3pm and 5pm
const start = Cypress.moment('3:00 PM', 'LT')
const end = Cypress.moment('5:00 PM', 'LT')

cy.get('.utility-moment .badge')
  .should(($el) => {
    // parse American time like "3:38 PM"
    const m = Cypress.moment($el.text().trim(), 'LT')

    // display hours + minutes + AM|PM
    const f = 'h:mm A'

    expect(m.isBetween(start, end),
      `${m.format(f)} should be between ${start.format(f)} and ${end.format(f)}`).to.be.true
  })
Posted at 3:38 PM

Cypress.Promise

To instantiate a new bluebird promise, use Cypress.Promise.

let waited = false

function waitOneSecond () {
  // return a promise that resolves after 1 second
  // eslint-disable-next-line no-unused-vars
  return new Cypress.Promise((resolve, reject) => {
    setTimeout(() => {
      // set waited to true
      waited = true

      // resolve with 'foo' string
      resolve('foo')
    }, 1000)
  })
}

cy.then(() =>
  // return a promise to cy.then() that
  // is awaited until it resolves
  waitOneSecond().then((str) => {
    expect(str).to.eq('foo')
    expect(waited).to.be.true
  }))