← Back to Cypress Mastery
Advanced18 min read

Performance & Optimization

Write fast stable E2E tests and reduce flakiness at scale.

Test Speed

Avoid unnecessary cy.visit—use cy.session to cache login. Stub heavy APIs with intercept. Disable video and screenshot except on failure in CI.

Prefer API setup for data preconditions over slow UI clicks through admin wizards.

  • cy.request is faster than UI for seeding state
  • Reduce defaultCommandTimeout only after suite stable
  • Run smoke subset on every commit; full suite nightly

Parallel Execution

Cypress Cloud parallelization distributes specs across machines by duration history. Self-hosted sorry-cypress or currents.dev alternatives exist.

Specs must be independent for parallel—no shared mutable accounts without unique data per spec.

cypress run --record --key $CYPRESS_RECORD_KEY --parallel --ci-build-id $GITHUB_RUN_ID

Flakiness

Flakes fail intermittently—timing, data collision, environment drift. Quarantine flaky specs with tag @flake and ticket; do not ignore permanently.

Cypress Cloud retries failed tests in run—use sparingly; fix root cause not mask with retries.

  • Track flake rate metric over time
  • Replace cy.wait(ms) with deterministic waits
  • Stabilize selectors and test data first before retry config

Resource Usage

Headless Electron uses less memory than headed Chrome. Close extra browser tabs not applicable in Cypress single tab model.

Large fixtures and uncleaned test data bloat database—truncate via task in afterEach.

afterEach(() => {
  cy.task("db:truncate");
});

E2E Best Practices

Test critical user journeys, not every CSS pixel. Complement with unit and integration tests for edge cases. Keep E2E count manageable—hundred slow tests hurt more than help.

Review suite duration weekly; delete redundant coverage. Align E2E with monitoring synthetic checks for production parity.

  • One assertion focus per test where possible for clear failures
  • Tag tests @smoke @regression for selective CI jobs
  • Document environment prerequisites in README for contributors

Get In Touch


Ready to discuss your next project? Drop me a message.