Search code examples
cypresse2ecypress-intercept

Cypress intercept does not match route with status 204


I am implementing cypress tests in our Angular application and have a problem waiting for a request to finish. I am guessing it has to do with the status of the Request being 204 instead of 200.

This is the function/command I am calling in my test:

export function logout() {
    cy.intercept('/api/security/logout').as('logoutRequest');
    cy.getCookie('SESSION').then((cookie) => {
        if (cookie != null) {
            cy.request(
                {
                    method: 'POST',
                    url: '/api/security/logout',
                }
            );
        }
    });
    cy.wait('@logoutRequest');
}

My problem is, that the route /api/security/logout is not recognized as alias @logoutRequest and therefore the wait always timeouts. Even though there is a vaild request. As you can see here in the test protocol: Test Failing No matches of route

I have tried modifing the route with * or ** but without success. I would be very glad if you could help me out.


Solution

  • You can't use cy.intercept() to catch cy.request().

    cy.intercept(), cy.server(), and cy.route()

    cy.request() sends requests to actual endpoints, bypassing those defined using cy.route() or cy.intercept()

    Just chain .then() off the request to handle the reply

    cy.request({method: 'POST', url: '/api/security/logout', failOnStatusCode: false})
      .then(response => {
        expect(response.status).to.eq(200)
      })