We use cy.intercept() to watch or change network requests during tests. This helps us check if our app talks to servers correctly.
cy.intercept() for request interception in Cypress
cy.intercept(method, url, response?)
method is the HTTP method like GET, POST, etc. It is optional.
url is the address or pattern of the request to catch.
cy.intercept('GET', '/api/users')
cy.intercept('POST', '/api/login', { statusCode: 200, body: { success: true } })
cy.intercept('/api/data', { fixture: 'data.json' })
This test fakes the server response for the user list. When the app asks for users, it gets one user named Alice. The test then checks if Alice's name appears on the page.
describe('Test user API', () => { it('should show mocked user data', () => { cy.intercept('GET', '/api/users', { body: [{ id: 1, name: 'Alice' }] }) cy.visit('/users') cy.contains('Alice').should('be.visible') }) })
You can intercept requests before the app sends them to control test conditions.
Use cy.wait() with intercept to wait for requests to finish.
Intercepting helps make tests faster and more reliable by avoiding real network calls.
cy.intercept() lets you watch or change network requests in tests.
It helps test how your app handles server data and errors.
You can fake responses to test different scenarios easily.
Practice
cy.intercept() in Cypress testing?Solution
Step 1: Understand the role of
This command is used to observe or change network requests made by the app during testing.cy.intercept()Step 2: Compare with other options
The other options describe unrelated testing tasks: creating UI elements dynamically, running tests in parallel, or generating random test data.Final Answer:
To watch or modify network requests during tests -> Option AQuick Check:
cy.intercept() = watch or modify requests [OK]
- Confusing cy.intercept() with UI commands
- Thinking it runs tests in parallel
- Assuming it generates test data
Solution
Step 1: Check correct parameter order and object structure
The correct order is method, URL, then response object with statusCode and body.Step 2: Validate each option
cy.intercept('GET', '/api/users', { statusCode: 200, body: [] }) matches correct syntax. cy.intercept('/api/users', 'GET', { statusCode: 200 }) swaps method and URL. cy.intercept('POST', '/api/users', { status: 200 }) uses wrong method and missing body. cy.intercept('GET', '/api/users', 200) passes status as number, not object.Final Answer:
cy.intercept('GET', '/api/users', { statusCode: 200, body: [] }) -> Option DQuick Check:
Method, URL, response object = cy.intercept('GET', '/api/users', { statusCode: 200, body: [] }) [OK]
- Swapping method and URL parameters
- Using status instead of statusCode
- Passing status as a number alone
cy.intercept('GET', '/api/data', { statusCode: 404, body: { error: 'Not found' } }).as('getData')
cy.visit('/data-page')
cy.wait('@getData').then(({ response }) => {
console.log(response.statusCode)
})Solution
Step 1: Analyze the intercept stub response
The intercept stubs the GET request to '/api/data' with statusCode 404 and error message.Step 2: Understand the console log output
Thecy.waitwaits for the intercepted request. The response object usesresponse.status(which is 404), but the code logsresponse.statusCode, which is undefined.Final Answer:
undefined -> Option CQuick Check:
response.statusCode = undefined (use response.status) [OK]
- Logging response.statusCode instead of response.status
- Confusing response.status with statusCode
- Expecting default 200 without stub
cy.intercept('GET', '/api/items', (req) => {
req.reply({ status: 201, body: { id: 1 } })
})Solution
Step 1: Check the reply object keys
The reply object must use 'statusCode' to set HTTP status, not 'status'.Step 2: Verify other parts
Method and URL are correct, and callback does not need to return a value.Final Answer:
Using 'status' instead of 'statusCode' in reply object -> Option BQuick Check:
Use statusCode, not status [OK]
- Confusing status with statusCode
- Thinking callback must return a value
- Assuming URL format is wrong
cy.intercept() usage correctly simulates a 2-second delay with a 200 status and a JSON body { success: true }?Solution
Step 1: Understand delay usage in cy.intercept
Delay, statusCode, and body can be used directly in the response object without a callback.Step 2: Check each option's correctness
cy.intercept('POST', '/api/login', { delay: 2000, statusCode: 200, body: { success: true } }) correctly uses delay, statusCode, and body directly. cy.intercept('POST', '/api/login', { delayMs: 2000, status: 200, body: { success: true } }) uses wrong keys 'delayMs' and 'status'. cy.intercept('POST', '/api/login', (req) => { req.reply({ delay: 2000, status: 200, body: { success: true } }) }) uses callback but 'status' instead of 'statusCode'. cy.intercept('POST', '/api/login', (req) => { req.delay(2000).reply({ statusCode: 200, body: { success: true } }) }) tries chaining a non-existent req.delay method.Final Answer:
cy.intercept('POST', '/api/login', { delay: 2000, statusCode: 200, body: { success: true } }) -> Option AQuick Check:
Direct { delay, statusCode, body } [OK]
- Using wrong keys like delayMs or status
- Using status instead of statusCode in callback
- Trying to chain non-existent req.delay method
Ready to prove it?
Pass the test and this pattern moves unknown
