https://cypress.io logo
You are on the right track, you may just have made...
# general-chat
n
You are on the right track, you may just have made a mistake in the reference. Try to console.log out the
response.body
first, then see what appears when you console.log out
response.body.messages
etc. until you get to the proper key. I cannot tell from the test code whether you are using intercept, but if you do there is a good chance you are confusing yourself with the naming. when you use intercept command and then use e.g.
cy.wait()
what you will get is an object that will contain both request and response. this means, that if you name your callback parameter
response
than you would end up with something like this:
Copy code
js
cy.wait('@myThing')
  .then( response => {
  console.log(response) // will contain response and request
  console.log(response.request) // will contain request
  console.log(response.response) // will contain your response
})
I’d suggest using different alias in the
.then()
callback parameter, so something like
.then( myThing => {})
or even better, use destructuring:
Copy code
js
cy.wait('@myThing')
  .then( ({response, request}) => {
  console.log(request) // will contain request
  console.log(response) // will contain your response
})
this way you don’t need to worry about naming things so much. I wrote an article on using destructuring and also on finding the right key in the response object, so that might give you some more useful info: https://filiphric.com/reading-and-testing-json-object-in-cypress https://filiphric.com/using-destructuring-in-cypress