Skip to content

Cypress Intercept Not Capturing Request Fix

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about Cypress Intercept Not Capturing Request Fix. We cover key concepts, practical examples, and best practices.

Your cy.intercept() doesn't capture the request you expect — the real API is called, or the wait for the alias times out.

The Problem

// WRONG — intercept registered after request was made
cy.visit('/users');
cy.intercept('GET', '/api/users').as('getUsers');
// The request was already sent when the page loaded
cy.wait('@getUsers'); // Timeout — never happened

The page initiates API calls during load. cy.intercept() is called after the visit, missing the request entirely.

Step-by-Step Fix

1. Register intercept before navigation

// RIGHT — intercept before triggering request
cy.intercept('GET', '/api/users').as('getUsers');
cy.visit('/users');
cy.wait('@getUsers');

2. Mock API responses

// RIGHT — stub API responses
cy.intercept('GET', '/api/users', {
  statusCode: 200,
  body: [
    { id: 1, name: 'Alice' },
    { id: 2, name: 'Bob' },
  ],
}).as('getUsers');

cy.visit('/users');
cy.wait('@getUsers');
cy.get('.user-item').should('have.length', 2);

3. Use intercept with route variations

// RIGHT — different HTTP methods
cy.intercept('POST', '/api/users').as('createUser');
cy.intercept('PUT', '/api/users/*').as('updateUser');
cy.intercept('DELETE', '/api/users/*').as('deleteUser');

4. Modify responses dynamically

// RIGHT — dynamic response modification
cy.intercept('GET', '/api/users', (req) => {
  req.continue((res) => {
    res.body.push({ id: 999, name: 'Mock User' });
    res.send();
  });
}).as('getUsers');

Expected output:

  ✓ intercepts and waits for API
  ✓ mocks API response
  ✓ dynamic response modification

Prevention Tips

  • Register intercepts before the action that triggers the request
  • Use cy.wait('@alias') to wait for intercepted requests
  • Use as('alias') to name intercepts
  • Use the response object to mock or modify API data
  • Use route variation patterns (GET, POST, DELETE)

Common Mistakes with intercept route

  1. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  2. Using return to exit a function early instead of wrapping a pure value in the monad
  3. Mixing let bindings with <- bindings in do notation, producing type errors

These mistakes appear frequently in real-world CYPRESS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What's the difference between cy.intercept and cy.route?

cy.intercept is the modern API (Cypress 6+) that supports Fetch API and all network types. cy.route is deprecated and only works with XHR. Always use cy.intercept.

How do I intercept GraphQL requests?

Match the URL and parse the request body: cy.intercept('POST', '/<a href="/apis/graphql/">graphql</a>', (req) => { if (req.body.operationName === 'GetUsers') { req.reply({ data: { users: [] } }); } }).

Can I intercept requests from specific pages only?

Yes. Use route patterns: cy.intercept('GET', '/api/users', { middleware: true }, (req) => { ... }) applies only to matching routes.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro