cypress: Failed to read the 'responseXML' property from 'XMLHttpRequest', `responseType` was 'text'

Example from docs produces error.

From https://docs.cypress.io/guides/guides/network-requests.html#Assertions

// spy on POST requests to /users endpoint
cy.route('POST', '/users').as('new-user')
// trigger network calls by manipulating web app's user interface, then
cy.wait('@new-user')
  .should('have.property', 'status', 201)

In my example, I used:

cy.wait('@create').should('have.property', 'status', 201);

Produces the following error:

InvalidStateError: Failed to read the 'responseXML' property from 'XMLHttpRequest': The value is only accessible if the object's 'responseType' is '' or 'document' (was 'text').

The following example works:

cy.wait('@create').then(({ status }) =>
  assert(status === 201, 'status is 201'),
);

Desired behavior:

Would like the example from the documents to work.

Preferably, I’d like cy.wait(...) to fail by default if a 200 isn’t returned, or to have a really easy way of adding our preferred status code to return. Something like cy.wait('@create', 201).

Versions

4.0.1

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 4
  • Comments: 16 (4 by maintainers)

Most upvoted comments

Hello

I had the same issue when upgrading cypress from 3.6.0 to 4.1.0.

Failing code was

cy.wait('@alias')
  .should('have.property', 'status', 200);

lead to the same error on response type.

Changing my code to :

cy.wait('@alias)
  .its('status')
  .should('eq', 200)

fixed the issue

The example from the website works for a standard XHR request and should work in your case, so there’s something amiss. The test below works for example.

it('route status', () => {
  cy.visit('https://example.cypress.io/commands/network-requests')
  cy.server()

  cy.route('GET', 'comments/*').as('getComment')

  cy.get('.network-btn').click()
  cy.wait('@getComment').its('status').should('eq', 200)
})

It appears it’s saying the responseType of your XHR request is text. It’s a bit weird that it’s warning this is only accessible is responseType is '' or document because '' defaults to text.

  • Ideally, can you provide us a test that we can run that reproduces this?
  • If not, can you run the test and when you see the error - click on the yellow error with DevTools open. Please take a screenshot of the full stack trace there and share it with us. I’d like to see where this error is throwing from.
  • Any insight into the responseType of your XHR request or any other info on what is unique about this request?

FWIW, I ran into this issue as well.

The offending line was:

cy.get('@alias').should('have.length', 1)

Changing it to this solved the problem:

cy.get('@alias').its('length').should('eq', 1)