Change a Response Body with cy.intercept to Provide Your Own Data in an API Call

Filip Hric
InstructorFilip Hric
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

With .intercept() command, we can change the response body. This enables us to provide our own response instead of the real one coming from server. We can then test our application’s behavior under different situations and test different edge cases.

Instructor: [0:00] Whenever my application is opened, it will fire this GET /api/boards request. This request will fetch the list of all of the boards. I can see it printed out in a console when I click on the line in the Cypress Runner. Here, I can see that the response body is an array of objects, and each of those objects will get rendered as an item in my list.

[0:25] With .intercept(), I can change this response body and, instead, provide my own data. I will type body, and we'll give that a value of empty array. When I now Save my test, I can see my application rendered as if I had no boards in my database.

egghead
egghead
~ 5 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today