1

I am using the method described in the answer of this question Log network failures in Cypress to log network failures. I basically intercept every failing request and its response and log it in some array as follows:

cy.intercept('*', (request) => {
    request.continue(response => {
      if(response.statusMessage !== "OK") {
        networkFails.push({request, response})
      }
    })
  })

The tests run perfectly fine, the problem is at the end of the tests I get this error enter image description here

How do I solve this problem?

Hesham Moneer
  • 240
  • 1
  • 17
  • It looks like a web socket error, but I don't think your intercept is handling those. You can verify by commenting out `request.continue()` and seeing if it goes away. – kegne Oct 24 '22 at 23:37
  • Yes the error goes away, but without the continue function, I won't be able to log responses. Is there a way to handle this? – Hesham Moneer Oct 25 '22 at 08:49
  • 2
    You would need to look into why this request is erroring, there may be clues on devtools network tab - please post any red lines listed there. It's expected behavior from Cypress, if a request fails and it has an intercept, Cypress sends that message. Note, the request is failing not the response so your code is still valid. – TesterDick Oct 25 '22 at 19:30
  • 1
    Is there a way I can bypass that error? Because I log these network failures to a file for later debugging anyway. – Hesham Moneer Oct 26 '22 at 08:57

1 Answers1

1

I ran into the same issue, and it turns out using request.continue is what was causing it.

request.continue is expecting a response, but when you get a socket hangup (ConnResetException), or in my case the socket being closed before it was finished writing, it triggers a cypress error.

Instead of request.continue, you'll want to use request.on( 'response', response => {

Your full code snippet should look like this:

cy.intercept('*', (request) => {
    request.on( 'response', response => {
      if(response.statusMessage !== "OK") {
        networkFails.push({request, response})
      }
    })
  })

The cypress documentation does a great job of explaining all of the different options here: https://docs.cypress.io/api/commands/intercept#Request-events

For reference, this was the specific cypress error I was getting while using .continue(). The error message was a bit different from yours but essentially the same problem requiring the same solution.

A callback was provided to intercept the upstream response, but a network error occurred while making the request:

Error: Socket closed before finished writing response
Jonathan Irvin
  • 896
  • 8
  • 17