0

I can't 'catch' requests to BE with Cypress. And even more, I can't see some XHR requests to BE, but they are in DevTools. I've added a screenshot with arrows to describe the problem better. enter image description here

I can't upload my project to the public repo, but maybe you can get some ideas based on the test itself. I don't have any beforeEach, etc.

it('should generate the right request for password change', () => {
cy.visit(`/courses/reset-password?token=${token}&userId=${userId}`);
cy.server();
cy.route('POST', '/auth/local/reset-password').as('resetRequest');
cy.get('#password').type(password);
cy.get('#confirmPassword').type(password);
cy.get('button[type="submit"]').click();
console.log('at the end');
cy.wait('@resetRequest').then((request) => {
  // never get here
  console.log('fff', request);
  console.log('requestBody', request.requestBody);
  expect(request.body.newPassword).to.eq(password);
  expect(request.body.token).to.eq(token);
  expect(request.body.userId).to.eq(userId);
});

});

If anyone has any ideas - please share them with me :)

andrey
  • 347
  • 3
  • 11
  • 1
    Specifying the URL as a string in `cy.route()` can be tricky. See https://docs.cypress.io/api/commands/route.html#url-as-a-string. Try using a glob pattern or regex instead. Also, make sure it's really an XHR and not a fetch. Cypress doesn't see fetch requests. – PeaceAndQuiet Apr 16 '20 at 02:18
  • @PeaceAndQuiet and is there any way to get 'fetch' or I should always rewrite 'fetch' to xhr to use cypress ? because I'm using fetch :( – andrey Apr 16 '20 at 19:32

2 Answers2

2

To be able to use cy.server() & cy.route() with fetch requests, you need to do as stated here: https://stackoverflow.com/a/49088084/9947826

PeaceAndQuiet
  • 1,692
  • 8
  • 13
0

I happened to find this code below, and worked fine on my end. Fetch calls are now displayed as XHR requests on the test logs.

Cypress.on("window:before:load", win => {
  win.fetch = null;
});

Source: https://github.com/cypress-io/cypress/issues/95#issuecomment-347607198

Batu
  • 1
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 21 '23 at 10:10