Cypress Intercept Not Capturing Request Fix
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
- Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad - 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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro