What if your tests never had to wait for a slow or broken server again?
Why Stubbing responses in Cypress? - Purpose & Use Cases
Imagine testing a web app that fetches data from a server. You have to wait for the server to respond every time you test. Sometimes the server is slow or down, so you can't test properly.
Manually waiting for real server responses is slow and frustrating. Tests can fail just because the server is unavailable, not because the app is broken. This makes testing unreliable and wastes time.
Stubbing responses lets you fake the server replies. You tell your test what data to expect without calling the real server. This makes tests fast, stable, and predictable.
cy.visit('/page'); cy.wait(5000); // waiting for real server response
cy.intercept('GET', '/api/data', { fixture: 'data.json' }); cy.visit('/page');
Stubbing responses enables fast, reliable tests that work even when the real server is slow or offline.
When testing a shopping site, you can stub the product list response to always show the same items. This way, your tests won't break if the real product database changes or is down.
Manual testing with real servers is slow and unreliable.
Stubbing responses fakes server data for fast, stable tests.
This helps catch real app bugs without external delays.
Practice
Solution
Step 1: Understand stubbing concept
Stubbing means faking server responses so the app can be tested without a real backend.Step 2: Identify purpose in Cypress
Cypress uses stubbing to isolate frontend tests from backend dependencies, making tests faster and more reliable.Final Answer:
To fake server responses so tests run without a real backend -> Option AQuick Check:
Stubbing = Fake server response [OK]
- Thinking stubbing speeds up browser rendering
- Confusing stubbing with test data generation
- Believing stubbing records user actions
Solution
Step 1: Identify correct Cypress command
The current Cypress command to stub network requests iscy.intercept().Step 2: Check method and parameters
Usingcy.intercept('GET', '/api/data', { fixture: 'data.json' })correctly stubs a GET request with a fixture response.Final Answer:
cy.intercept('GET', '/api/data', { fixture: 'data.json' }) -> Option CQuick Check:
Use cy.intercept for stubbing requests [OK]
- Using cy.stub instead of cy.intercept
- Using deprecated cy.route command
- Using cy.mock which does not exist in Cypress
cy.intercept('GET', '/users', { body: [{ id: 1, name: 'Alice' }] }).as('getUsers');
cy.visit('/users');
cy.wait('@getUsers').its('response.body[0].name').should('equal', 'Alice');Solution
Step 1: Analyze the intercept stub
The intercept stubs a GET request to '/users' with a response body containing one user named 'Alice'.Step 2: Check the wait and assertion
The test waits for '@getUsers' and asserts the first user's name equals 'Alice', which matches the stub.Final Answer:
Test passes because the stubbed response matches 'Alice' -> Option AQuick Check:
Stub response matches assertion = pass [OK]
- Assuming alias is undefined without .as()
- Thinking response body is empty
- Confusing syntax of cy.intercept
cy.intercept('POST', '/login', { statusCode: 200, body: { token: 'abc123' } });
cy.visit('/login');
cy.wait('@loginRequest');Solution
Step 1: Check alias usage
The intercept is defined but not given an alias using .as(), so '@loginRequest' does not exist.Step 2: Understand wait on alias
Waiting on '@loginRequest' fails because the alias was never created.Final Answer:
The alias '@loginRequest' is missing because the intercept was not aliased -> Option DQuick Check:
Alias missing causes wait failure [OK]
- Forgetting to add .as() to intercept
- Thinking POST is invalid method
- Believing response body must be string
Solution
Step 1: Understand dynamic stubbing need
Different responses based on query parameters require conditional logic in the stub.Step 2: Use function handler in cy.intercept
cy.intercept supports a function to inspect the request and send custom responses based on req.query.category.Final Answer:
Use cy.intercept with a function handler to check req.query.category and send different responses -> Option BQuick Check:
Function handler enables dynamic stubbing [OK]
- Trying multiple intercepts with same URL (only last applies)
- Ignoring query parameters in stub
- Using deprecated cy.route instead of cy.intercept
Ready to prove it?
Pass the test and this pattern moves unknown
