Home > Back-end >  Cypress test passes and does not wait for cy.request()
Cypress test passes and does not wait for cy.request()

Time:01-05

In one of my tests I need to make two request (second request depends on first response) and after that make some assertions. The problem is that test passes before getting response from server

Test File

it('My test', () => {
  makeReqs().then((res)=> {
    console.log(res);
    someAssertions
  })
})

Request functions

function first() {
  return Promise((resolve) => {
   something
   resolve('response')
  })
}

makeReqs() {
  return new Cypress.Promise((resolve) => {
    first().then((res) => {
      //here I use res in resObj
      cy.request({reqObj}).then(response => resolve(response.data))
    })
  }
}

Can someone help me? Still have some problems with asynchronous Cypress

CodePudding user response:

When using promise/resolve within a Cypress test, add Mocha done() method to indicate when the test is done.

Cypress monitors promises on it's own queue, but you have not wrapped the promise in a command so it can't tell when the test is done.

it('waits for done', (done) => {
  const promise = makeReqs()
  promise.then((data) => {
    //test the data
    done()
  })
})

Or add an async/await to the test, or wrap in a custom command to allow Cypress to monitor the promise.

  • Related