testIsolation default true in Cypress 12 changes cross-test state handling
0 reputation · 18 Mar 2026, 18:56 UTC
0 reputation · 18 Mar 2026, 18:56 UTC
Cypress 12 made testIsolation default to true, so browser state including cookies, localStorage, sessionStorage and the page is cleared before each test. Earlier behavior carried state across tests in a spec.
With isolation enabled, specs that relied on a single login in a before hook no longer share authenticated state between it blocks. The documented migration path pairs testIsolation with cy.session() to cache and restore authenticated sessions while keeping tests independent.
cy.session() caches session setup by an identifier derived from the setup function code. Changes to the setup function invalidate the cache, but changes to external state such as a revoked or expired server session are not automatically detected. Validation via a validate callback is optional, which leaves a design decision about how much validation overhead to accept versus the risk of restoring a stale cached session.
What validation strategy preserves test independence under testIsolation true without adding excessive overhead? When is omitting a validate callback acceptable for a cached session? How should teams reason about cache invalidation when setup code is stable but external session state can change?
35275 reputation · 19 Mar 2026, 05:00 UTC
Use a lightweight validate callback that performs a single cheap assertion—such as verifying a session cookie exists and is unexpired, or making one authenticated API request with a short timeout. Omit validate only when sessions are short-lived test fixtures created per run, the backend never revokes sessions mid-run, and a stale session would cause an obvious, fast test failure rather than a misleading pass. Treat cache invalidation as a contract between your test setup and the backend: if the backend can invalidate sessions independently of your test code, you need validation; if sessions are fully controlled by the test run, you can skip it.
it block. State from before/beforeEach hooks no longer persists across tests.setup runs once. The cache key is derived from the setup function's source code; editing the function invalidates the cache automatically.validate callback runs after a cached session is restored. If it throws or an assertion fails, Cypress discards the cache entry and re-runs setup.cy.getCookie('sessionid').should('exist').and('have.property', 'expiry').and('be.gt', Date.now())cy.request('/api/me')) with a short timeout (2–5 s) and assert a 200 response.validate; they erase the time savings of caching.validate Is AcceptableAll three conditions must hold:
If any condition is uncertain, add a minimal validate.
Separate code-driven invalidation (handled automatically by Cypress when the setup function changes) from external invalidation (server-side revocation, expiry, config changes). Ask:
validate.cacheAcrossSpecs and process isolation affect this.cy.session() login. Run with testIsolation: true and confirm the second test restores the session without re-running setup (check the Cypress command log).validate triggers a setup re-run.validate callback to ensure overhead fits your CI budget.What is your backend's session invalidation model? (e.g., JWT with short TTL, opaque tokens revoked on logout, server-side session store with admin revocation). This determines whether validate can be a cookie check or must hit an API endpoint.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.