Cypress · Browser testing end to end

How does network interception work in Cypress?

MediumnetworkinterceptionproxyUpdated Oct 2026
Quiz this track

Short answer

cy.intercept registers a route that observes or modifies matching requests and responses through Cypress's proxy. It can stub a response, inspect the request, or alias the call so a test can wait for it.

Cypress routes traffic through its proxy, which lets cy.intercept see requests the application makes. A route matches on method and URL, and a handler can reply with a stub, continue to the real server, or inspect and modify the request. The route applies to requests made after it is registered.

ts
cy.intercept("GET", "/api/products", (req) => {
  req.reply({
    statusCode: 200,
    body: [{ id: 1, name: "Desk lamp" }],
  });
}).as("getProducts");

cy.visit("/products");
cy.wait("@getProducts");
cy.get("[data-cy=product]").should("have.length", 1);
The route stubs the products call, and the alias makes the test wait for that specific request.

What interception is good for

  • Stubbing a slow or unavailable third-party service
  • Forcing error and empty states that are hard to reproduce
  • Waiting for a request instead of guessing with a timeout
  • Asserting that the app sent the right payload
  • Recording a real response and replaying it in a deterministic test

Register intercepts before the action that triggers the request. If the app fires the call on page load, the intercept must be set before cy.visit. Interception observes the browser's requests; it does not reset server data or replace a proper API test.

Do not intercept everything by default. Over-stubbing hides integration bugs. Stub the boundary you do not control, and let your own API run when the test is meant to verify integration.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1When must an intercept be registered relative to cy.visit?
  2. 2What is the difference between stubbing and spying on a request?
  3. 3How do you force an error state?
  4. 4Why not stub every network call?

Say it in the interview

cy.intercept registers a route through Cypress's proxy. I can reply with a stub, let the real request through, or inspect and modify it. I register routes before the action that triggers them and alias them so the test can wait precisely. I stub boundaries I do not control, but I avoid stubbing everything because that hides real integration problems.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Cypress mock interview and get scored feedback on depth, clarity and confidence.