# Utilities

Examples of the use of methods from other commonly used libraries in Cypress, for a full reference of commands, go to docs.cypress.io

#### Cypress._.method()

To call an underscore method, use the `Cypress._.method()` command.

    cy
      // use the _.chain, _.pluck, _.first, and _.value functions
      // https://on.cypress.io/api/cypress-underscore
      .request('http://jsonplaceholder.typicode.com/users').then(function(response){
        var _ = Cypress._
        var ids = _.chain(response.body).pluck("id").first(3).value()

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

#### Cypress.$(selector)

To call a jQuery method, use the `Cypress.$(selector)` command.

    var $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.moment()

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

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

    cy.get('.utility-moment').contains(time)

Posted at 3:38 PM

#### Cypress.Blob.method()

To work with blobs, convert strings, and other utility functions, use the `Cypress.Blob.method()` command.

    // get the dataUrl string for the javascript-logo
    // https://github.com/nolanlawson/blob-util#imgSrcToDataURL
    return Cypress.Blob.imgSrcToDataURL(
      '/assets/img/javascript-logo.png',
      undefined,
      'anonymous'
    ).then(function(dataUrl){
      // create an <img> element and set its src to the dataUrl
      var img = Cypress.$('<img />', {src: dataUrl})

      // need to explicitly return cy here since we are initially returning
      // the Cypress.Blog.imgSrcToDataURL promise to our test
      return cy
        .get('.utility-blob').then(function($div){
          // append the image
          $div.append(img)
        })
        .get('.utility-blob img')
          .click()
          .should('have.attr', 'src', dataUrl)
    })

#### new Cypress.Promise(function)

To instantiate a new bluebird promise, use the `new Cypress.Promise(function)` command.

    var waited = false

    function waitOneSecond(){
      // return a promise that resolves after 1 second
      return new Cypress.Promise(function(resolve, reject){
        setTimeout(function(){
          // set waited to true
          waited = true

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

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