03 / 05

How do we modify Request or Response Dynamically

Difficulty: 5/10
cy.intercept, request/response stubbing, dynamic modification

​

cy.intercept() lets you:​
  1. 1

    Cypress intercepts a POST request to /api/login.

  2. 2

    It doesn’t block the request immediately — instead, it uses a callback to manipulate the response.

  3. 3

    In this case, it fakes the login token in the reply.

You want to customize responses on the fly, or perhaps simulate a successful login without hitting a real auth server.

Scenario Questions

0-2 years experience

  1. 1We have a login page that sends a POST to /api/auth. How would you use Cypress to change the username field in the request before it reaches the server?
  2. 2During a test you need the API to return a 200 with a custom JSON payload instead of the real data. Walk me through the code you’d write to achieve that.

2-5 years experience

  1. 1Our test suite intermittently fails because a third‑party service sometimes returns extra fields. How would you intercept the response and strip those fields dynamically?
  2. 2You added a cy.intercept handler to modify a request, but the app now shows a CORS error. What could be going wrong and how would you fix it?

5-8 years experience

  1. 1We want to centralize request/response mutation for all tests in a large monorepo. What design would you propose for reusable intercept logic, and how would you handle versioning of the mutation rules?
  2. 2When modifying responses on the fly, how do you ensure that performance isn’t degraded for a suite that runs hundreds of tests in parallel?

8+ years experience

  1. 1Our organization is migrating from Cypress v9 to v12, and we have dozens of custom intercepts scattered across teams. How would you plan a migration strategy that minimizes breakage and keeps test reliability high?
  2. 2Discuss the trade‑offs of embedding business‑logic decisions (e.g., feature flags) inside Cypress intercepts versus handling them in the application code. How does this affect cross‑team ownership and long‑term maintenance?

Follow-up Questions

  • What edge cases can cause a modified response to break downstream UI logic?
  • How would you debug a test that suddenly stops intercepting a request?
  • When would you prefer a fixture over programmatic mutation?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.