Stubbing responses in Cypress - Build an Automation Script
describe('User list with stubbed API response', () => { it('should display stubbed users Alice and Bob', () => { cy.intercept('GET', '/api/users', { statusCode: 200, body: [ { id: 1, name: 'Alice' }, { id: 2, name: 'Bob' } ] }).as('getUsers') cy.visit('http://localhost:3000') cy.wait('@getUsers') cy.get('[data-test=user-list]').should('contain.text', 'Alice') cy.get('[data-test=user-list]').should('contain.text', 'Bob') }) })
This test uses cy.intercept() to stub the GET request to /api/users. We provide a fixed response with two users, Alice and Bob.
We give this intercept an alias @getUsers so we can wait for it to complete with cy.wait(). This ensures the page has received the stubbed data before we check the UI.
We visit the page URL to trigger the API call. Then we check the user list element (selected by data-test=user-list) contains the names Alice and Bob.
This approach avoids flaky tests by synchronizing on the network call and uses semantic selectors for maintainability.
Now add data-driven testing with 3 different stubbed user lists and verify the UI for each
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
