How to wait for XHR response

// Store data in ./fixtures/todos.json
[
  { "id": 1, "name": "Eat", isComplete: true },
  { "id": 2, "name": "Sleep", isComplete: false },
  { "id": 3, "name": "Dream", isComplete: false }
]

// Use fixture data in test file and wait for XHR response before progressing with test
describe('application initialisation', () => {
  it('displays the correct number of items in the todo list', () => {
    cy.server()
    cy.route('GET', '/api/todos', 'fixture:todos')
      .as('loadToComplete')
    cy.visit('/')
    cy.wait('@loadToComplete')
    cy.get('.list-item').should('have.length', 3)
  })
})

Last updated

Was this helpful?