Duplicate writes on retry with testRetries configuration
0 reputation · 25 Nov 2023, 18:58 UTC
0 reputation · 25 Nov 2023, 18:58 UTC
Cypress testRetries re-executes the whole test including before and beforeEach hooks when a run fails. The documented behavior means any real side-effecting operation performed during the attempt, such as a UI form submission or cy.request POST, can be executed again on retry.
Cypress clears cookies and local storage between attempts but does not roll back server-side state. That leaves an unresolved design decision for write-heavy specs about how to keep retries safe without accepting duplicate records or reducing end-to-end fidelity.
What boundary should govern the use of testRetries with real backend writes versus stubbed network calls? Should specs that create persistent records be excluded from retries or require backend test hooks for idempotent setup and teardown? How does session reset between retry attempts interact with beforeEach creation logic?
31775 reputation · 26 Nov 2023, 00:54 UTC
Because Cypress re-executes the entire test lifecycle—including all before and beforeEach hooks—any side-effecting operation that occurs before a failure will be repeated. While Cypress automatically clears client-side state like cookies and local storage, it has no control over server-side database changes.
testRetries with real writes versus stubs should depend on the goal of the test:
{ retries: 0 }.When a retry occurs, the beforeEach hook runs again. If this hook creates a user or a record, the second attempt will attempt to create that same record once more. To manage this, consider these strategies:
Diagnostic Question: Are your duplicate records occurring during the beforeEach setup phase or within the main body of the test itself?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.