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.
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.
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.
e2e: { defaultCommandTimeout: 10000 },How to prevent it
- Use stable
data-cyselectors that exist in the CI build. - Wait on intercepted requests rather than fixed sleeps.
- Tune
defaultCommandTimeoutto the slowest CI runner.