0

I'm stuck trying to run a test as a registered and logged in user with a custom command that sends a POST request and body.

Then in the test suite in a beforeEach hook I try to create user and then log in:

beforeEach(() => {
    cy.createUser().then((user) => {
      cy.login(user);
    });
  });

Whenever I run the test cypress trhows error:

CypressError: `cy.request()` failed on:

http://localhost:3000/register

The response we received from your web server was:

  > 404: Not Found

This was considered a failure because the status code was not `2xx` or `3xx`.

If you do not want status codes to cause failures pass the option: `failOnStatusCode: false`

This is what I do inside the custom command:

cy.visit('/').then(() => {
    cy.request({
      url: '/register',
      method: 'POST',
      body: user,
    }).then((response) => ({ ...response.body.user, ...user }));
  });

Why is this happening ? Please any feedback will be greatly appreciated. I've also tried with the flag failOnStatusCode: false but didn't work.

Many thanks.

accdias
  • 5,160
  • 3
  • 19
  • 31
Francis Lagares
  • 81
  • 5
  • 11
  • 1
    It seems like you are trying to call a wrong URL, the message indicates a 404 not found. – Amine Safi Aug 27 '21 at 14:21
  • If I open that URL in the browser it works, so I don't understand why doesn't find it in headless mode. By default Cypress assumes your defined baseUrl in cypress.json file. – Francis Lagares Aug 27 '21 at 15:55
  • @FrancisLagares Have you found any solution to this problem? I am facing exactly the same issue. – Vimal Patel May 22 '23 at 16:40

0 Answers0