How do I wait until a cookie is set?
05:19 17 Feb 2019

I am writing the acceptance tests for my application's login feature. At some point, I want to double-check the cookie's expiry time.

Upon clicking on the "Login" button, a graphql query is sent to my server which responds with a Jwt. Upon reception of the jwt, the application sets the cookie with

document.cookie = ...

In my Cypress test, I check the token in the following way:

Then("sa session s'ouvre pour {SessionDurationType}", expectedDuration => {
  cy.get('@graphql').then(() => {
    cy.wait(1000)
    cy.getCookie('token').then(cookie => {
      const tokenDuration = getTokenDuration(cookie.value)
     expect(tokenDuration.asSeconds()).to.equal(expectedDuration.asSeconds())
    })
  })
})

With cy.get('@graphql'), I am waiting for the graphql query to return a response. The alias is defined like this:

cy.stub(win, 'fetch', fetch).as('graphql')

Upon reception, the application sets the cookie.

My problem is that I am not fond of the following call:

cy.wait(1000)

Without that call, I always get an undefined cookie.

Is there a way to get that cookie within some time that might be much less than 1000 ms? I tried many things without success...

javascript cookies cucumber graphql cypress