W3cubDocs

/Cypress

Cypress.Promise

Cypress automatically includes Bluebird and exposes it as Cypress.Promise.

Instantiate a new bluebird promise.

Syntax

new Cypress.Promise(fn)

Usage

Correct Usage

new Cypress.Promise((resolve, reject) => { ... })

Incorrect Usage

new cy.Promise(...)  // Errors, cannot be chained off 'cy'

Examples

Use Cypress.Promise to create promises. Cypress is promise aware so if you return a promise from inside of commands like .then(), Cypress will not continue until those promises resolve.

Basic Promise

cy.get('button').then(($button) => {
  return new Cypress.Promise((resolve, reject) => {
    // do something custom here
  })
})

Waiting for Promises

it('waits for promises to resolve', function () {
  let waited = false

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

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

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

© 2017 Cypress.io
Licensed under the MIT License.
https://docs.cypress.io/api/utilities/promise.html