How do you wait for a request and check it through an alias?
A route is named with .as(), and cy.wait('@alias') waits for the next unconsumed request of that name and yields an interception object. The test checks what the page sent and received, without pauses. cy.wait does not check success, since a 500 is also a response, so the status is checked separately. If it reports no request, the method or URL of the route is usually wrong.
Cypress · Network
cy.wait('@orders') waits and fails after 5 seconds although the order was created. What is wrong?
After clicking “Place order” the page shows a success message, and the test checks only that. The backend may have received an empty cart or answered 201 with a wrong body, and the UI will not show it. An alias lets you wait for the request itself and check what the frontend sent and received. The lesson explains how this works and why cy.wait sometimes fails although the request happened.
How it works and why. .as('orders') gives the route a name. From then on every request that matches the route is recorded by Cypress under that name. cy.wait('@orders') takes the next request not yet consumed, waits for it to finish and yields an interception object with request and response. The first cy.wait takes the first match, the second one with the same alias takes the second, and so on. So two calls are needed when the page made two requests.
The order of checking a request
Two timeouts decide how long cy.wait waits: requestTimeout (5 seconds by default) for the request to appear and responseTimeout (30 seconds) for the response. If the request never appeared, the message says no request with that alias was made, which is almost always a wrong method or URL in the route.
Example: from a pause to checking the request.
cy.wait(2000) and a UI check
The pause guesses the time and says nothing about what the page sent. The request could carry an empty cart and the “Success” message would still appear.
an alias and a request/response check
The test waits for the request itself and checks the body and the status. There are no pauses, and the step finishes as soon as the response arrives.
cy.intercept('POST', '/api/orders').as('createOrder');
cy.get('[data-cy=checkout]').click();
cy.wait('@createOrder').then(({ request, response }) => {
expect(request.body.items).to.have.length(2);
expect(response?.statusCode).to.eq(201);
});
// the same through its: the check is retried until the timeout
cy.get('@createOrder')
.its('response.statusCode')
.should('eq', 201);
Here cy.wait yields the interception and .then checks the request body (the number of items) and the response status. A 500 is also a response, so cy.wait does not fail in that case: checking the status is up to you.
Limits and alternatives. Several requests can be waited for with an array: cy.wait(['@a', '@b']) waits for both. GraphQL sends everything to one URL, so the alias is set dynamically in a handler: if (req.body.operationName === 'Login') req.alias = 'login'. An alias lives only within a test: routes are reset before every test, so register them again in each test, for example in beforeEach.
There are no aliases: the wait for a request is created before the action and awaited after it.
const orderResp = page.waitForResponse('**/api/orders');
await page.getByTestId('checkout').click();
const resp = await orderResp;
expect(resp.status()).toBe(201);
expect(resp.request().postDataJSON().items).toHaveLength(2);Common mistakes.
- An extra
cy.wait(2000)next tocy.wait('@alias'): the alias already waits exactly as long as needed. - The method or URL in the route does not match the real request (
/api/ordersagainst/api/v2/orders/):cy.waitwaits for a request that does not exist. - One
cy.waitfor two requests: the second stays unchecked, and a latercy.waitwith that alias will take it rather than a new one. - Assuming
cy.waitchecks success: it only waits, the status must be checked separately.
Check yourself
A page calls GET /api/items twice (the initial load and a refresh after a click). What does each of two cy.wait('@items') yield?
Answer
The first yields the interception of the first request, the second that of the second. Each cy.wait with one alias takes the next match not yet consumed.
The user sees the order in the UI, but cy.wait('@createOrder') says there was no request. What do you check first?
Answer
The route match: method, URL and the time of registration. Often the route is written as POST /api/orders while the app calls /api/v2/orders/, or the route was registered after the action. The Cypress command log shows the real request.
cy.intercept with an alias, perform the action and call cy.wait on the alias. It returns the request and the response, so I check the body and the status. Middle/Senior: Instead of pauses I wait for a specific request, and tune requestTimeout and responseTimeout if the backend is slow. For GraphQL I set an alias by operationName. I check the status explicitly, because cy.wait also passes on a 500.