✓ Recommended
Cypress E2E Testing Patterns
Cypress for end-to-end testing with component testing, network stubbing, custom commands, and CI integration.
CLAUDE.md
# Cypress E2E Testing Patterns
You are an expert in Cypress, end-to-end testing, and browser-based test automation.
Architecture:
- E2E tests in cypress/e2e/: full browser tests against running application
- Component tests in cypress/component/: isolated component rendering
- Custom commands in cypress/support/commands.ts: reusable test helpers
- Fixtures in cypress/fixtures/: static test data (JSON files)
Selectors:
- Use data-cy attributes for test selectors: [data-cy="submit-button"]
- Avoid CSS classes and IDs (fragile, change with design updates)
- Use cy.contains() for text-based selection (user-visible)
- Use cy.findByRole, cy.findByLabelText with @testing-library/cypress plugin
- Chain commands for specificity: cy.get('[data-cy=form]').find('input')
Network:
- Use cy.intercept() to stub API responses (no real backend needed)
- Mock specific routes: cy.intercept('GET', '/api/users', { fixture: 'users.json' })
- Wait for requests: cy.intercept('/api/data').as('getData'); cy.wait('@getData')
- Test error states: cy.intercept('/api/save', { statusCode: 500 })
- Test loading states: cy.intercept('/api/data', (req) => req.reply({ delay: 2000 }))
Custom Commands:
- Create reusable login command: Cypress.Commands.add('login', (email, password) => {...})
- Use for repetitive setup: cy.seedDatabase(), cy.resetState()
- Type custom commands in cypress/support/index.d.ts for TypeScript
- Avoid excessive abstraction: commands should be self-explanatory
Best Practices:
- Never use cy.wait(ms) for timing: use cy.intercept + cy.wait('@alias') instead
- Each test should be independent: reset state in beforeEach
- Use baseUrl in cypress.config.ts to avoid hardcoded URLs
- Use cy.session() for caching login state across tests
- Use retries for flaky tests in CI: retries: { runMode: 2, openMode: 0 }
CI Integration:
- Use cypress run for headless CI execution
- Record to Cypress Cloud for parallelization and failure screenshots
- Use start-server-and-test to start app before running tests
- Set video: false in CI to speed up runs (unless debugging failures)
- Upload screenshots and videos as CI artifacts on failure
Add to your project root CLAUDE.md file, or append to an existing one.