geo
geo

Reputation: 2433

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.

Upvotes: 5

Views: 4738

Answers (1)

geo
geo

Reputation: 2433

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.

Upvotes: 5

Related Questions