--- name: cypress-ops description: "Cypress end-to-end and component testing: selector strategy, network stubbing, auth, flake diagnosis, CI. Use for: cypress, e2e test, component test, cy.get, cy.intercept, cy.session, data-cy, data-test, retry-ability, flake, flaky test, cypress.config, cy.mount, Test Replay, custom commands, fixtures." when_to_use: "Use when writing or fixing Cypress tests - e.g. 'pick stable selectors', 'share login with cy.session'. For Playwright or other runners, use playwright-ops / test-engineering." license: MIT allowed-tools: "Read Write Bash" metadata: author: claude-mods related-skills: "playwright-ops, test-engineering, ci-cd-ops" --- # Cypress Operations > Facts verified as of 2026-07. **Version context (verified against docs.cypress.io, 2026-06):** Cypress 14.x, Test Replay (v13+), `cy.session` with `cacheAcrossSpecs`. APIs move — confirm against the live docs when a detail is load-bearing. End-to-end and component testing with Cypress (`cypress`, TS/JS). The runner executes tests *inside* a real browser via the **Cypress App** (`cypress open`) or headlessly (`cypress run`). The defining mental model: **`cy.*` commands are not promises** — they enqueue onto an async command chain that Cypress drains for you. Internalise that and the agentic gotchas below disappear. ## Quick Start ```bash npm install -D cypress npx cypress open # launch the Cypress App: pick E2E or Component, real browser npx cypress run # headless run, all specs (CI default) npx cypress run --spec "cypress/e2e/auth/*.cy.ts" npx cypress run --component # run component specs npx cypress run --browser chrome --headed npx cypress run --record --key # upload to Cypress Cloud (enables Test Replay, v13+) ``` Specs live in `cypress/e2e/**/*.cy.ts` (E2E) and beside components or `cypress/component/` (component). Config is a single `cypress.config.ts` at the repo root. ## The Async Command Queue (read this first) `cy.get(...)` returns a **Chainer**, not the element and not a Promise. Commands are *scheduled*, then run in order after the test function returns. This is the source of nearly every Cypress mistake an agent makes. ```ts // WRONG — cy.get does not return a value; `el` is a Chainer, this is meaningless const el = cy.get('[data-test=total]'); if (el.text() === '$0') { /* never works */ } // WRONG — async/await does nothing useful; cy commands aren't awaitable promises const text = await cy.get('[data-test=total]'); // do NOT do this // RIGHT — yield the value into a callback; assertions inside .should() retry cy.get('[data-test=total]').should('have.text', '$0'); // RIGHT — need the raw value? use .then() (but it does NOT retry — see below) cy.get('[data-test=total]').invoke('text').then((text) => { // text is a string here; runs after the queue reaches this point }); ``` Rules that follow from this: - **No `const`/`let` to "store" a command result.** Use `.as()` aliases + `cy.get('@alias')`. - **No `async/await` on `cy.*`.** The queue handles ordering. Mixing in real promises? wrap them with `cy.then(() => promise)` or `cy.wrap(promise)`. - **No `if/else` on element state read synchronously.** Conditional testing is an anti-pattern in Cypress (the DOM may not have settled); make the app deterministic, or drive the branch off a server/`cy.intercept` state you control. Deep dive: [references/network-and-auth.md](references/network-and-auth.md). ## Retry-ability (why you almost never need waits) Cypress retries **queries** and **assertions** until they pass or the command times out (default 4s). It does **not** retry **actions** (`.click()`, `.type()`, `.select()`) — those fire once, though the queries *leading up to* them retry until the element is actionable (visible, not disabled, not animating). | Construct | Retries? | Use for | |-----------|----------|---------| | `cy.get` / `.find` / `.contains` / `.its` / `.invoke` (queries) | Yes — whole chain re-queries | Locating/reading DOM that may not be ready | | `.should(...)` / `expect` inside it | Yes — the callback re-runs | Assertions; conditional waits on settled state | | `.click` / `.type` / `.select` (actions) | No — fire once | Interactions (leading queries still retry) | | `.then(cb)` | **No** — runs once, no retry protection | Extracting a value; NOT for assertions | ```ts // .should(callback) retries the whole callback — safe for racy DOM cy.get('[data-test=rows] li').should(($li) => { expect($li).to.have.length(3); expect($li.first()).to.contain('Alice'); }); // .then() does NOT retry — capturing $el here then asserting later races the render ``` If you reach for `cy.wait(3000)`, you're missing an assertion or an aliased intercept. The only legitimate `cy.wait` takes an **alias** (`cy.wait('@getUsers')`), never a number. ## Selector Strategy **Prefer a dedicated test attribute over CSS classes, IDs, or tag names** — the latter are brittle and change with styling/refactors. Cypress recommends `data-cy` **or** `data-test` (the Cypress Real World App standardises on **`data-test`**); pick one and enforce it. ```ts // GOOD — decoupled from styling and structure cy.get('[data-test=submit]').click(); // AVOID — couples the test to CSS/markup that changes for non-test reasons cy.get('.btn-primary').click(); cy.get('#submit').click(); ``` Wrap the convention in a custom command so specs stay terse: ```ts // cypress/support/commands.ts Cypress.Commands.add('getBySel', (sel, ...args) => cy.get(`[data-test=${sel}]`, ...args)); Cypress.Commands.add('getBySelLike', (sel, ...args) => cy.get(`[data-test*=${sel}]`, ...args)); // substring match // usage: cy.getBySel('submit').click(); ``` Reserve `cy.contains('Log In')` for when the **visible text itself** is what you're asserting; otherwise it couples tests to copy. ## Network Stubbing — `cy.intercept` `cy.intercept` is the single API for spying on and stubbing network traffic. **Set it up before the action that triggers the request**, alias it, then wait on the alias. ```ts // Stub with a fixture, alias, wait cy.intercept('GET', '/api/users', { fixture: 'users.json' }).as('getUsers'); cy.visit('/users'); cy.wait('@getUsers'); // resolves when the request fires // Inline body / status cy.intercept('POST', '/api/login', { statusCode: 401, body: { error: 'nope' } }).as('login'); // routeMatcher object (method + glob/regex url) + dynamic reply cy.intercept({ method: 'GET', url: '/api/orders/*' }, (req) => { req.reply((res) => { res.body.hasMore = false; }); // tweak the real response }).as('orders'); // Assert against the captured request/response cy.wait('@login').its('response.statusCode').should('eq', 401); // Wait on several at once cy.wait(['@getUsers', '@orders']); ``` **Stub what you don't own, exercise what you do.** Stubbing third-party/slow endpoints makes tests fast and deterministic; hitting your real backend (seeded via `cy.request`) verifies the client↔server contract. Decide per endpoint. GraphQL, request modification, and seed-via-`cy.request` patterns: [references/network-and-auth.md](references/network-and-auth.md). ## Authentication — `cy.session` Log in **once**, cache the session, restore it across tests (and optionally specs). This is the biggest suite-speed win after stubbing. ```ts // cypress/support/commands.ts Cypress.Commands.add('login', (username: string, password: string) => { cy.session( [username, password], // cache key — array/object is stringified () => { // setup: runs only on cache miss cy.visit('/login'); cy.get('[data-test=name]').type(username); cy.get('[data-test=password]').type(password); cy.get('form').contains('Log In').click(); cy.url().should('contain', '/dashboard'); // assert logged-in before caching! }, { validate() { // runs after setup AND after each restore cy.getCookie('auth_token').should('exist'); // invalid -> setup re-runs }, cacheAcrossSpecs: true, // default false; true = reuse in every spec }, ); }); ``` Critical behaviour: **cookies, `localStorage`, and `sessionStorage` across all domains are cleared before `setup` runs, regardless of `testIsolation`.** Faster still: skip the UI and log in via `cy.request` inside `setup`, persisting the token. Patterns (API login, token priming, `cy.origin` for cross-origin SSO): [references/network-and-auth.md](references/network-and-auth.md). ## Component vs E2E Testing Same runner, two testing types. **E2E** drives a deployed app through `cy.visit`. **Component** mounts a single component in a real browser via `cy.mount` — no server, no navigation, props/events under direct control. | | E2E | Component | |---|---|---| | Entry | `cy.visit('/path')` | `cy.mount()` | | Needs running app server | Yes | No (bundler dev server only) | | Spec location | `cypress/e2e/**/*.cy.ts` | beside the component / `cypress/component/` | | Support file | `cypress/support/e2e.ts` | `cypress/support/component.ts` (registers `cy.mount`) | | Best for | User flows, integration, auth | Props/events/slots, edge states, visual | ```ts // cypress/support/component.ts (React example) import { mount } from 'cypress/react'; Cypress.Commands.add('mount', mount); // Button.cy.tsx cy.mount(