QA Interview Hub · Knowledge base · Quizzes · Українська

Cypress › Network: cy.intercept

cy.intercept

cy.intercept has three parts: what to catch, what to do and what to call it with .as(). A route is matched by method and URL (string, glob or regex), or by an object with query or headers for the same URL. Without a response it is a spy, with a static response a stub, and a handler changes the request or response. It sees only page HTTP requests, not WebSocket or cy.request.

Cypress · Network

What call forms does cy.intercept have and when to take which: a string, an object, a handler?

In a project one test waits for a request, a second replaces a response, a third tells GraphQL requests apart, and each is written differently. This lesson gathers the call forms of cy.intercept so you can see which fits which task; each is covered in detail in the neighbouring lessons of the chapter.

How it works and why. cy.intercept has three parts: what to catch (method, URL, other conditions), what to do (nothing, a static response or a handler) and what to call it (.as()). That is enough to cover observing, stubbing and changing a request or a response with one tool. Conditions can be given as a string, a glob, a regular expression or an object, and the response or the handler goes in as the last argument: third after a method and a URL, second after an object.

Two ways to describe a match

String or glob

Quick and short

A method and a URL in one call: cy.intercept('GET', '/api/users*'). That is enough for most tests.

A RouteMatcher object

Finer conditions

Method, URL, query, headers and times in one object when the URL is the same but the requests differ.

Figure 1. Both ways are equal: an object is needed when a string is not enough.

All typical tasks in one table:

TaskCallWhat happens
Wait for a requestcy.intercept(m, url).as('x')a spy, the request goes to the server
Replace a responsecy.intercept(m, url, { fixture })a stub, no server
Change a request(req) => { req.headers[…] = … }then goes to the server
Change a responsereq.continue((res) => …)the real response, edited
Tell requests apartreq.alias = 'x'an alias in a handler (GraphQL)

Example: an object instead of a string.

// page 2 of the list, not any page
cy.intercept({
  method: 'GET',
  pathname: '/api/users',
  query: { page: '2' },
}).as('usersPage2');

// GraphQL: one URL, different operations
cy.intercept('POST', '/graphql', (req) => {
  if (req.body.operationName === 'Login') {
    req.alias = 'login';
  }
});

// a network failure
cy.intercept('GET', '/api/users', { forceNetworkError: true });

The first route catches only the second page of the list, because the URL is the same and the page parameter tells requests apart. The second only sets an alias in a handler and replaces nothing, so the request goes to the server. The third simulates a dropped connection.

Limits and alternatives. The route must be declared before the action, and cy.intercept itself sees only HTTP requests of the page (not WebSockets and not cy.request). It lets you test the UI apart from an unstable backend but does not confirm the backend itself: that needs API tests. When there are many tests, shared calls are moved into a custom command or beforeEach so routes are not copied.

And in Playwright:

One function page.route(url, handler) with the actions fulfill, continue, abort and fallback; for waiting for a request there are separate waitForRequest and waitForResponse.

Common mistakes.

  • Taking a string when the URL matches several requests: the stub answers the wrong request.
  • Forgetting .as() and then hunting for the request in the log instead of cy.wait.
  • Relying on a stub instead of checking the real backend: the contract may change and the test will not notice.

Check yourself

A user list is requested with ?page=1 and ?page=2 on one URL. How do you stub only the second page?

Answer

Pass an object instead of a string: { method: 'GET', pathname: '/api/users', query: { page: '2' } } with the response as the second argument. The first page then goes to the real server.

How do you tell two GraphQL requests apart when the URL and the method are the same?

Answer

Read req.body.operationName in a handler and set req.alias for the needed operation, then wait with cy.wait('@login').

In an interview. Junior: cy.intercept catches HTTP requests by method and URL. It is used to wait for a request, replace a response and check what was sent. Middle/Senior: For finer conditions I take an object with query, headers and times, and for GraphQL I set an alias in a handler. I simulate errors and a slow network through statusCode, delay and forceNetworkError. I keep part of the tests without mocks so the real backend is checked too.

More articles in this chapter