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

Cypress › Network: cy.intercept

How do you replace or change a server response?

A response is replaced with a static object as the third argument, or with a handler that calls req.reply() or req.continue(cb). A static response (body or fixture, statusCode, delay, forceNetworkError) suits a 500 error, an empty list or a slow network. After reply the request never reaches the server, while continue lets it through and the callback only edits the real response. The stub body shape comes from the contract, not guesswork.

Cypress · Network

How do you check that the UI shows a 500 error and a slow network when the backend works without failures?

A statistics page has a “Could not load” banner and a spinner for a slow response. A real server does not produce such states on demand. cy.intercept does: you can answer with an error, delay the response or change just one field of the real one. The lesson covers three ways and when to choose which.

How it works and why. The third argument can be a static response: body or fixture, statusCode, headers, delay, throttleKbps or forceNetworkError. When you need logic you pass a handler req => { … } instead. In it req.reply() answers itself and the request does not go to the server. req.continue(res => …) lets the request reach the server and lets you change the real response before the page gets it. A handler that calls none of these passes the request to the next match and finally to the server.

Three ways to respond

Static response (3rd argument)the server never sees the requestA handler and req.reply()the server never sees the requestA handler and req.continue(cb)the server answers, we change the response
Figure 1. The difference is whether the request reaches the server and where the response comes from.

Example: states you cannot trigger on a real backend.

Static

A fixed state

Fits when the response is always the same: a 500 error, an empty list, a slow network.

Handler

State depends on the request

Needed when the response depends on the request body or parameters or only part of the real one changes.

// server error
cy.intercept('GET', '/api/stats', {
  statusCode: 500,
  body: { error: 'boom' },
});

// slow network
cy.intercept('GET', '/api/stats', {
  fixture: 'stats.json',
  delay: 2000,
});

// the real response, with one field replaced
cy.intercept('GET', '/api/stats', (req) => {
  req.continue((res) => {
    res.body.total = 0;
  });
});

The first two examples do not touch the server: they are pure stubs. The third lets the request reach the server and changes only total, the other fields are real. For a JSON response res.body is already parsed into an object. All three routes for one URL are shown together here only for comparison; in one test the one registered last would win.

Limits and alternatives. A static response is faster and fully controlled but easily drifts from the real contract: the shape of the error body, field names, date formats. A response through req.continue stays closer to the real one but depends on the backend. delay simulates a delayed response, not real bandwidth: for that there is throttleKbps. To cut the connection use forceNetworkError: true. Take the fixture body from a real response or the API schema rather than inventing it.

And in Playwright:

Replacing is route.fulfill(), passing through is route.continue(), cutting is route.abort(). To change the real response use route.fetch() and fulfill with a new body.

await page.route('**/api/stats', async (route) => {
  const response = await route.fetch();
  const json = await response.json();
  json.total = 0;
  await route.fulfill({ response, json });
});

Common mistakes.

  • A stubbed error with a body of a different shape than the real API: the UI test passes and in production the banner is not shown.
  • Calling req.reply() and then expecting the request to reach the server: after reply the response has already been given.
  • Changing res.body outside req.continue or a res callback: at that point there is no response yet.
  • Leaving delay in a shared beforeEach: every test gets slower for no reason.

Check yourself

What is the difference between req.reply() and req.continue(cb) for a request that exercises backend discount logic?

Answer

After req.reply() the server never sees the request, so the discount logic does not run: the test only checks how the UI reacts to an invented response. req.continue(cb) sends the request to the server, the real backend calculates the discount, and the callback can only adjust the response.

A test with the stub { statusCode: 500, body: { error: 'boom' } } is green, and in production a 500 shows an empty banner. What was missing from the test?

Answer

The stub body did not match the real error format (for example, the app reads message, not error). The body shape should come from the contract or a real response.

In an interview. Junior: For simple cases I pass the response as the third argument: a fixture, a status, a delay. That is how I check how the UI behaves on a 500 or a slow network. Middle/Senior: If I need to change a real response precisely, I write a handler with req.continue: the server works and I change one field. I take the shape of the error body from the contract so the stub does not drift from reality.

More articles in this chapter