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

Send Tweet
Published 3 months ago
Updated 2 months 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.