Cypress - cy.wait() timed out waiting 5000ms for the 1st request to the route

Viewed 10862

Fairly new to Cypress and it's been smooth sailing so far until I wanted to fake out a network request. Code is like this:

describe('Some test', function(){
it('Can fake out an XHR response', function(){
    cy.server()
    cy.route('https://reqres.in/api/users', [{ id: 1, name: 'Pat' }]).as('getMessages')
    cy.request('https://reqres.in/api/users')
    cy.wait(['@getMessages'])

...

No matter what I try I get:

CypressError: Timed out retrying: cy.wait() timed out waiting 5000ms for the 1st request to the route: 'getMessages'. No request ever occurred.

What am I doing wrong here?

EDIT: I changed the code to this:

cy.server()
cy.route('https://reqres.in/api/users', [{ id: 1, name: 'Pat' }]).as('getMessages')
cy.request('https://reqres.in/api/users')
cy.wait(['@getMessages'])

But the result was as below:

1 Answers

As of Cypress 6.0.0 cy.intercept() is the successor to cy.route() and can be used to return an expected response as follows:

cy.intercept('GET','/api/users', expectedResult).as('interceptedRequest');

cy.wait('@interceptedRequest');

https://docs.cypress.io/api/commands/intercept

Related