Skip to content

Cypress Best Practices

This lesson consolidates the most important best practices from across this course into a single, practical reference you can revisit as you build real Cypress suites.

The Core Principles

Nearly every specific best practice in Cypress traces back to a small number of core principles: prefer stable selectors, prefer assertion-based waiting over fixed delays, keep tests independent, and control the network layer deliberately rather than accepting whatever a real backend happens to return.

// A test that follows all four core principles
beforeEach(() => {
  cy.loginAs('user@example.com', 'pw'); // independent, cached via cy.session()
});

it('shows the cart total after adding an item', () => {
  cy.intercept('GET', '/api/cart', { fixture: 'cart.json' }).as('getCart');
  cy.visit('/cart');
  cy.wait('@getCart');
  cy.get('[data-cy="cart-total"]').should('have.text', '$50.00');
});

Stable selector, assertion-based waiting, independent setup, and controlled network response, all four principles in one small test.

The Four Core Principles

1. Stable selectors (data-cy) over brittle CSS/structure
2. Assertion-based waiting over fixed delays
3. Independent tests, no shared or order-dependent state
4. Deliberate network control via cy.intercept()
  • Selectors should target purpose-built attributes, not styling or auto-generated markup.
  • Waits should react to real, observable conditions, not guessed durations.
  • Every test should be runnable alone, in any order, without depending on another test's side effects.
  • Network responses should be deliberately controlled for most feature-focused tests.

Best Practices Quick Reference

A condensed checklist covering this course's most important recommendations.

Area Best Practice
Selectors Use data-cy/data-testid, avoid styling classes
Waiting Use .should() and cy.wait('@alias'), avoid fixed cy.wait(ms)
Test independence Reset state in beforeEach, avoid order dependence
Authentication Use cy.session() + API login, avoid repeated UI logins
Network Stub with cy.intercept() for most feature tests
Assertions Prefer .should() over .then() + expect() where possible
Custom commands Extract repeated logic, name by intent not implementation
Test data Use fixtures/factories, avoid hardcoded colliding values

Test Independence, Revisited

Of all the principles covered in this course, test independence is arguably the one that most determines whether a suite remains trustworthy as it grows. A suite where tests can run in any order, individually or in parallel, and still pass reliably, is a suite teams actually trust.

  • Reset relevant backend state in beforeEach(), not just once per file.
  • Never rely on a previous test having created data a later test depends on.
  • Verify independence periodically by running suspect tests in isolation and in randomized order.

Reviewing Tests With the Same Rigor as Application Code

Best practices only compound in value when they're consistently applied, which requires treating test code review with the same rigor as application code review: checking selector quality, waiting patterns, and independence, not just whether the test happens to pass.

Common Mistakes

  • Treating best practices as optional polish rather than the primary driver of long-term suite reliability.
  • Applying these principles inconsistently across a codebase, leaving pockets of brittle, flaky tests.
  • Reviewing new tests for correctness alone without also reviewing them for selector quality and independence.
  • Not revisiting older tests written before the team adopted these practices consistently.

Key Takeaways

  • Nearly all Cypress best practices trace back to four core principles: selectors, waiting, independence, and network control.
  • Test independence is often the single most important factor in long-term suite trustworthiness.
  • Reviewing tests with the same rigor as application code keeps these practices consistently applied.
  • This cheat sheet is meant as an ongoing reference, not a one-time read.

Pro Tip

Turn this lesson's cheat sheet into an actual code review checklist for your team, pull requests that add or modify Cypress tests get reviewed against it explicitly, this is far more effective at maintaining consistency than relying on everyone remembering best practices from a course.