cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
46.68k stars 3.16k forks source link

errors when requesting API #29913

Open LuizVithor opened 1 month ago

LuizVithor commented 1 month ago

Current behavior

I'm making requests in an api but when I'm using Cypress it returns error 400 or net::ERR_EMPTY_RESPONSE on a specific route with a specific method, but when I access it in a normal browser, other than Cypress, it works normally.

Literally the same request but in Cypress it gives an error...

in some cases it gives the error "net::ERR_EMPTY_RESPONSE" and in others it gives a 400 error bringing an api error but the same request in the normal browser works.

My cypress.config.ts:

import dotenv from 'dotenv';
dotenv.config();
import { defineConfig } from "cypress";

export default defineConfig({
  e2e: {
    baseUrl: process.env["NEXT_PUBLIC_URL_CYPRESS"],
    setupNodeEvents(_on, config) {
      config.env = {
        ...process.env,
        ...config.env
      }
      return config
    },
    reporter: 'mochawesome',
    reporterOptions: {
      charts: true,
      json: true,
      reportsDir: 'reports/your-reports-folder',
      overwrite: true
    },
    chromeWebSecurity: false,
    experimentalModifyObstructiveThirdPartyCode: false,
    modifyObstructiveCode: false
  },
  projectId: "MYPROJECTID",
  chromeWebSecurity: false,
  experimentalModifyObstructiveThirdPartyCode: false
});

I noticed that most, if not all requests that give an error, in the normal browser return 204 no content

image image image image image image

Desired behavior

it should have just been successful and the interception returned a common request with statuscode 204, as 204 is not an http error

Test code to reproduce

I'm not going to have a test code, but basically I was simulating a website and one of the requests I needed to make, returned 204, and I realized that all that returned 204 gave an error

Cypress Version

13.13.1

Node version

v20.12.1

Operating System

windows 11

Debug Logs

No response

Other

No response

D0d0d commented 1 month ago

I had similar problem those weeks and in the end it was about queuing cy commands since they are async. I moved my wait in "cy.request.toTrigger.then" block and specified which exactly response to wait.

cy.request().then(res=>{
....
})

If it suits your case, I found where to look for my conditions here: Docs: cy.request(Yields)

LuizVithor commented 1 month ago

So, from what I understand, the error is only in 204 requests, instead of it returning the status code "204" in the interception, it returns an error, for now I'm aligning it with my back end so it doesn't send 204 anymore, but this It bothers me because 204 is not an error, but a success

jennifer-shehane commented 1 month ago

We'd need a way to test this ourselves to narrow it down. It does seem strange.