What is cy.intercept and where exactly does the interception happen?
cy.intercept captures the HTTP requests the page makes and lets a test observe or replace them. When the method and URL match, Cypress either records the exchange (a spy), returns its own response (a stub), or lets the request through and allows changing the real response. It sees traffic leaving the browser (fetch, XHR, third-party scripts), but not WebSocket messages or cy.request calls.
Cypress · Network
A test depends on an outside API that is sometimes slow and sometimes down. How do you make it stable?
A product page loads data from a backend that on the test environment is sometimes slow and sometimes returns somebody else’s data. The test goes red for a reason that is not a UI bug. cy.intercept lets you see the request the page made and, when needed, answer in place of the server. This lesson explains what it sees, how it works and where its abilities end.
How it works and why. You register a route: a method, a URL (a string, a glob or a regular expression) and, if needed, other conditions. Every HTTP request of the page is compared with the routes. When a route matches, Cypress either just records the exchange (a spy), or returns its own response (a stub), or lets the request reach the server and lets you change the real response. The application code does not have to be touched: what left the browser is intercepted, so fetch, XHR, libraries and third-party scripts are all visible alike.
The path of a request
According to the Cypress documentation, since version 16 interception in Chrome, Chromium and Edge happens on the browser’s native network layer, while Firefox, WebKit and Electron still send traffic through a proxy in the Cypress Node process. For a test there is almost no difference: the API is the same in every browser.
Example: from a backend dependency to controlled data.
Two modes of one route
Observe
The request goes to the real server. Cypress only records the request and the response, and you can check them through an alias.
Replace
The request never reaches the server: the test supplies the response (fixture, body, status, delay).
A test that relies on the real backend checks the number of products the environment returned and breaks when the data changes:
// depends on the data of the environment
cy.visit('/products');
cy.get('[data-cy=product]').should('have.length', 3);
With a stub the test sets the data itself, and an alias lets it wait for the request:
cy.intercept('GET', '/api/products*', {
fixture: 'products.json', // three products
}).as('products');
cy.visit('/products');
cy.wait('@products');
cy.get('[data-cy=product]').should('have.length', 3);
// spy: the request goes to the real server, we only watch
cy.intercept('GET', '/api/profile').as('profile');
Both calls have a method and a URL. The glob /api/products* also matches a query string (?page=2) but not /api/products/12, because * does not cross /.
Limits and alternatives. cy.intercept works with HTTP requests of the page. It does not intercept WebSocket messages. Requests made by cy.request are not seen either: Cypress makes them from Node, not the page. A stub does not prove that the real backend returns the same data: if a fixture drifts from the contract, the UI test stays green and production breaks. So keep at least a few end-to-end tests against the real backend and check fixtures against the API schema.
page.route() intercepts the requests of a page and context.route() those of the whole context. Unlike Cypress, a WebSocket can also be intercepted and mocked, through page.routeWebSocket() (since version 1.48).
Common mistakes.
- Registering the route after
cy.visitor a click: the request has already gone, andcy.waittimes out. - A glob that is too wide (for example
**/api/**) with a fixture: every request gets that response, not just one. - Mocking everything and checking nothing against the real backend: the test verifies a fixture, not the system.
- Expecting
cy.requestor a WebSocket to be intercepted: routes do not see those channels.
Check yourself
A test registers cy.intercept('GET', '/api/orders').as('orders') after cy.visit('/orders'), and cy.wait('@orders') times out although the page showed orders. Why?
Answer
A route catches only requests made after it was registered. The page had already requested /api/orders while loading. Register the route before cy.visit or before the action that triggers the request.
Will cy.intercept see a cy.request('/api/orders') call from the same test?
Answer
No. cy.request is executed by Cypress from Node, bypassing the page, while cy.intercept works with requests the page makes. A test’s own API calls do not need interception: the response comes back to the test anyway.
cy.intercept intercepts the HTTP requests of the page. You can only observe and check the request and the response (a spy) or replace the response with a fixture or a body (a stub). It does not intercept WebSockets. Middle/Senior: I register the route before the action. I use a spy to wait for the request and check what was sent, and a stub for states that are hard to reach on a real backend: an error, an empty list, a slow response. I check fixtures against the contract and keep a few tests without mocks so I do not stay green on invented data.