Why must an intercept be set before the action, and what happens if there are several on one URL?
A route acts only on requests that started after it was registered, so it is set before cy.visit or the click. When several routes match, Cypress goes from the newest, and a route that responds ends the search, so a later one overrides an earlier one. The times: 1 option removes a route after one match, which gives the failure-then-success scenario. All routes are reset before every test.
Cypress · Network
Two intercepts on one URL: which one answers, and how to make “first a failure, then success”?
A shared beforeEach sets a stub for /api/stats with a list, and one test must check the “Try again” button: the first request must fail, the second must pass. The test gets an error on every request or on none. The lesson explains when a route starts to act, how one of several is chosen and what the times option is for.
How it works and why. A route acts only on requests that started after it was registered. A request that has already gone will not be seen by it: if you register the route after cy.visit or a click, cy.wait times out, and this is a classic cause of flaky tests. When several routes match a request, Cypress goes through them from the newest to the oldest. A route with a response (a static response or req.reply) or with req.continue ends the search. A handler that only looks at the request passes it on. So a route registered later overrides an earlier one: that is how a test overrides the general stub from a shared beforeEach.
Which route answers
Example: “first an error, then success”.
beforeEach(() => {
cy.intercept('GET', '/api/stats', {
fixture: 'stats.json',
}).as('stats');
});
it('the “Try again” button repeats the request', () => {
// a newer route: fires once and is dropped
cy.intercept(
{ method: 'GET', url: '/api/stats', times: 1 },
{ statusCode: 503 },
).as('statsFail');
cy.visit('/stats');
cy.wait('@statsFail');
cy.contains('Could not load');
cy.contains('button', 'Try again').click();
cy.wait('@stats');
cy.get('[data-cy=total]').should('be.visible');
});
The first request matches the newer route and gets a 503. After one match times: 1 removes it, and the second request reaches the route with the fixture. Without times every request would get a 503, because the newer route stands first.
Limits and alternatives. All routes are reset before every test, including those created in before(): in the second and later tests they are gone, so shared routes go in beforeEach. If the response depends on the request number, a counter in a handler can replace several routes, but it is harder to read than times. A chain of handlers that only change the request is handy for headers, when the final route answers itself.
The rules are similar: the route must be registered before the action, the last registered handler runs first, you can pass the request to the next one with route.fallback(), and the { times: 1 } option exists too.
Common mistakes.
- Registering the route after
cy.visit: the request slipped through andcy.waittimed out. - Creating a shared route in
before(): in later tests it is gone. - Not adding
timesto the failure route: “Try again” can never succeed because every request gets a 503. - Expecting the older route to run first: the order is reversed, newest first.
Check yourself
A beforeEach sets a stub for /api/user with status 200, and the test later adds the same route with status 404 and no times. Which status does the page get?
Answer
404. The search goes from the newest route, it answers itself, and the older one with 200 does not act while the newer one exists.
A route is created in before(), the first test is green and the second fails on cy.wait. Why?
Answer
Cypress resets all routes before every test. In the first test the route is still there, in later ones it is gone, so the alias is empty. Create the route in beforeEach or in the test itself.
visit or a click. If there are several routes on a URL, the last registered one runs first. Middle/Senior: I keep general stubs in beforeEach and override them in the test with what that test needs. For a “first a failure, then success” scenario I add times: 1 to the failure route. I remember that routes are reset before every test.