Skip to content
tezvyn:

cy.intercept: Control Network Traffic in Cypress Tests

Source: docs.cypress.ioHardHow cards are made

cy.intercept: Control Network Traffic in Cypress Tests

cy.intercept acts like a programmable proxy in your Cypress tests, letting you watch, modify, or fake network requests. Use it to test loading states or error handling without a live backend. Footgun: intercepts are cleared before each test, not just once.

Why it exists

Modern web apps are constantly making network requests. To test frontend behavior reliably, you need to control the responses from those requests. Waiting for a real backend is slow, flaky, and makes it hard to simulate specific edge cases like network errors or empty data sets.

The mental model

Think of cy.intercept() as a traffic cop standing between your application and the network. It can simply watch requests go by (spying), stop a request and provide a fake response (stubbing), or even change the request before letting it proceed. This gives you complete control over the network layer during your tests.

How it works

You define an intercept by specifying what to match and what to do. Matching can be a simple URL string, a glob pattern, or a complex RouteMatcher object with properties like method, hostname, and headers. Once matched, you have three options. First, do nothing, which just spies on the request. Second, provide a StaticResponse object to immediately return a canned response, like { statusCode: 404, body: 'Not Found' }. Third, provide a routeHandler function that receives the request object (req), allowing you to inspect it, modify it, and dynamically decide what response to send back.

When to use it

Use cy.intercept() to test how your UI reacts to different network conditions. For example, stubbing a response to show a loading spinner, then an error message when the request fails with a 500 status code. It's also perfect for providing consistent data to your tests, ensuring they don't break if the backend database changes. You can also use it to wait for a specific request to complete before making an assertion with cy.wait('@alias').

When not to use it

Avoid cy.intercept() for true end-to-end tests where you explicitly want to verify the contract between your frontend and a live backend. Over-stubbing can create tests that pass even when the real application is broken because the frontend and backend have drifted apart. For these scenarios, let requests go through to a real, controlled test environment.

One canonical example

To test a 'user not found' scenario, you can intercept a GET request and force a 404 response. First, intercept the route: cy.intercept('GET', '/api/users/123', { statusCode: 404 }).as('getUser'). Then, visit the page: cy.visit('/users/123'). Next, wait for the call: cy.wait('@getUser'). Finally, assert the UI changed: cy.contains('User not found').should('be.visible'). This verifies the UI correctly handles the error state.

Interview question

Which scenario best illustrates a potential pitfall of over-relying on cy.intercept for all network interactions in Cypress tests?

  • a.It becomes impossible to test dynamic request modifications or error handling scenarios effectively.
  • b.Test execution becomes significantly slower due to the overhead of intercepting every request.
  • c.The application's UI might not accurately reflect real-world network latency or server response times.
  • d.Tests could pass even if the frontend and backend APIs have diverged, masking real integration issues.Correct
Why?

The card explicitly states that "Over-stubbing can create tests that pass even when the real application is broken because the frontend and backend have drifted apart," highlighting the risk of masking integration issues. While cy.intercept can simulate latency, its primary pitfall when overused is losing true end-to-end contract verification.

Just read this? Test yourself on what you have been reading.

Read the original → docs.cypress.io

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles