Use cy.server() and cy.route() to control API response in a cypress test

Share this video with your friends

Send Tweet

Our tests should not be brittle and break just before the data returned from the API changed slightly. As an example - imagine testing Twitter with their ever-changing feed.

When writing e2e tests it's important to have a known state of the app at the beginning of each test so that potential failures depend specifically on the thing we're trying to test.

In this lesson we're going to learn how to use cy.server() and cy.route() to control API response in a cypress test.

Informatique Editoriale
Informatique Editoriale
~ 4 years ago

Hi, i would like to know if it's possible with route (or route2 ?) to mock a fetch on a backend api like :

fetch("http://localhost:3001/api/posts")

i try with cy.server() and cy.route2(), but nothing is mocked

Informatique Editoriale
Informatique Editoriale
~ 4 years ago

just for information, it works now using Cypress 6.0

cy.intercept("http://localhost:3001/api/posts")