Skip to content
Latchkey

Cypress "Timed out retrying after 4000ms" element assertion in CI

Cypress retries commands and assertions until they pass or the command timeout (4000ms by default) elapses. A failure means the element never appeared or the assertion never held in that window, commonly because CI renders slower than your machine or the data is not ready.

What this error means

A test fails with "Timed out retrying after 4000ms: Expected to find element: [data-cy=submit], but never found it." Green locally, red in CI, sometimes green on rerun.

cypress
Timed out retrying after 4000ms: Expected to find element:
`[data-cy=submit]`, but never found it.

Common causes

The element renders slower than the timeout in CI

Async data or a slower CI machine means the element appears after 4000ms, so the retrying command runs out.

A selector that does not match the CI build

A selector tied to text, locale, or flags that differ in CI never matches, so retries exhaust the timeout.

How to fix it

Assert on the state the app actually reaches

Wait for the data or intercepted request to complete before asserting on the element it produces.

cypress/e2e/orders.cy.js
cy.intercept('GET', '/api/orders').as('orders');
cy.visit('/orders');
cy.wait('@orders');
cy.get('[data-cy=submit]').should('be.visible');

Raise the command timeout for slow CI

Increase defaultCommandTimeout so genuinely slow renders are not misreported as failures.

cypress.config.js
e2e: { defaultCommandTimeout: 10000 },

How to prevent it

  • Use stable data-cy selectors that exist in the CI build.
  • Wait on intercepted requests rather than fixed sleeps.
  • Tune defaultCommandTimeout to the slowest CI runner.

Frequently asked questions

What causes ""Timed out retrying after 4000ms""?
Async data or a slower CI machine means the element appears after 4000ms, so the retrying command runs out.
How do I fix "Timed out retrying after 4000ms"?
Wait for the data or intercepted request to complete before asserting on the element it produces.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card