Stub an API Request Status Code and Error Message with cy.intercept

Filip Hric
InstructorFilip Hric

Share this video with your friends

Send Tweet
Published 3 months ago
Updated 2 months ago

Frontend application can react differently to various server responses. One way to test the application behavior is to change response status code. .intercept() command in Cypress has the ability to stub the status code and error message that the server would provide on error.

Instructor: [0:00] Besides simulating a general network error, I can change the status code of my API request. When I type status code 500 and Save my test, you can see that the error code is rendered in my application.

[0:19] The app also renders the error message that the API will provide. If I want to change this fallback message into something else, I can type body and the property message will be "Oops, something went wrong." When I Save my test, you'll see that the message is rendered over here.