Search code examples
javascriptjsonautomationcucumbercypress

How to solve this "Failed to execute 'json' on 'Response': body stream already read" issue?


enter image description here

TypeError: Failed to execute 'json' on 'Response': body stream already read

The following error originated from your application code, not from Cypress. It was caused by an unhandled promise rejection.

Failed to execute 'json' on 'Response': body stream already read

When Cypress detects uncaught errors originating from your application it will automatically fail the current test.

This behavior is configurable, and you can choose to turn this off by listening to the uncaught:exception event


Solution

  • When you see an error that says uncaught errors originating from your application it means that the app itself has thrown an error, but not handled it.

    It's not something that the test is doing wrong, but you can tell Cypress not to complain about the issue by using the code from here To conditionally turn off uncaught exception handling for a certain error

    Cypress.on('uncaught:exception', (err, runnable) => {
      if (err.message.includes("Failed to execute 'json' on 'Response")) {
        return false
      }
    })
    

    (this is placed at the top of your spec, preferably before cy.visit()).


    However, in your case the error in the app is simply that the Response: json() static method is being used

    The json() method of the Response interface takes a Response stream and reads it to completion.

    but the error is telling you that the response stream has already been read, and is presumably already converted to JSON format.

    So, removing the .json() static method should solve your problem.