Stubbing API Calls in Cypress Tests with cy.intercept
Learn how to use Cypress.io's cy.intercept to stub API calls, create deterministic UI tests, and avoid backend‑induced flakiness while understanding the trade‑offs.
ReadMeFeed / Community knowledge
Real questions. Useful conversations. Find the people who know your stack.
Learn how to use Cypress.io's cy.intercept to stub API calls, create deterministic UI tests, and avoid backend‑induced flakiness while understanding the trade‑offs.
Cypress retries commands and assertions automatically. Learn how to replace arbitrary cy.wait() calls with retryable queries and assertions, and where the mechanism stops working.
Cypress 'detached from DOM' failures are stale references, not missing elements. A cause table, ordered checks, and fixes that keep the query next to the action.
Learn how to use Cypress cy.session() to cache authentication state across tests, eliminate repeated UI logins, and keep your suite fast without hiding login regressions.
Cypress cy.session() can eliminate redundant logins, but only if you disable testIsolation. Learn the trade-offs, version requirements, and how to verify the cache works.
Learn how to use cy.intercept() to stub API responses in Cypress, eliminating backend flakiness and enabling the testing of edge cases like 500 errors and empty states.
Fixed cy.wait() calls make Cypress suites slow and still flaky. Retry-ability, network aliases, and retrying assertions replace guesswork with deterministic waiting.
Stub XHR/fetch at the browser boundary with cy.intercept() to make UI tests deterministic — registering, verifying, and recovering from silent pass-through.
Decide when to stub API calls with cy.intercept() or hit the real endpoint with cy.request() in Cypress. A practical guide compares speed, reliability, maintenance, and contract validation, then shows concrete test snippets and how to measure trade‑offs.