Mock Your Backend with Cypress

Share this video with your friends

Send Tweet

Using cy.server, cy.route, and cy.wait, we can intercept requests to our real backend, and return known mock responses.

The pros of using this approach:

  • It's much faster than interacting with the real backend
  • Doesn't need to seed data to the backend
  • Easy control of requests and responses
  • Can mock network delay and failure

Cons:

  • No guarantee the stubs match the real backend responses
  • Not full end-to-end testing

Recommended Homework:

  • Mock the route to create a new todo, and reply with a 200
  • Mock the route to edit an existing todo, and reply with a 200
Joshua Travis
Joshua Travis
~ 5 years ago

For some reason, cy.route() doesn't want to use my configured baseUrl by default. So I'm having to use Cypress.config().baseUrl to get it to work. Any thoughts as to why?