Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypress: Cannot stub request with cy.route()

I have the below spec file :

describe('The First Page', () => {
  beforeEach(() => {
    cy.server();
    cy.route({
      method : 'GET',
      url: '**/v3/user/*',
      status: 204,
      response: {
        id: 1,
        firstName: 'Dev',
        lastName: 'Dev',
        email: '[email protected]',  
      },
    });
  });
  it('successfully loads', () => {
    cy.visit('/dashboard/account');
  });
});

By reading the cypress documentation, I thought that this is the way we can stub a http request to the web server. This is not working though as I expected since the http request to 'api/v3/user' returns 504 status. Is there a way to mock/stub a http request using any command of cypress ? I am using the

whatwg-fetch

module in order to make request in my application.

like image 616
geo Avatar asked Feb 26 '18 11:02

geo


People also ask

How do I find my route in Cypress?

You can test a route multiple times with unique response objects by using aliases and cy. wait(). Each time we use cy. wait() for an alias, Cypress waits for the next nth matching request.

What is stubbing in Cypress?

Replace a function, record its usage and control its behavior.

What is the difference between Cy request and Cy intercept?

cy. intercept does not make a request, but rather "listens" to requests that occur on the network layer. If we "ask" Cypress to name a certain request that we expect to occur after some action, we can also "ask" it to wait for it before moving on when it notices that such a request occurred. That is, cy.


1 Answers

I finally succeed to resolve the issue. The main reason that I could not stub a request is that I was using on my React-app the

window.fetch

method from the 'whatwg-fetch ' module. As I saw here, https://github.com/cypress-io/cypress/issues/687 there is an issue with the 'fetch' method. So, when I changed the fetch to axios all problems were solved.

like image 105
geo Avatar answered Sep 30 '22 13:09

geo