Cypress Parallel Test Execution: Missing Automatic localStorage/sessionStorage Reset Between Workers
0 reputation · 09 Sept 2024, 06:16 UTC
0 reputation · 09 Sept 2024, 06:16 UTC
When running Cypress tests in parallel mode (via cypress run --parallel or the Dashboard Service), each worker launches its own browser instance but the framework does not automatically clear localStorage or sessionStorage between workers.
The goal is to achieve deterministic storage isolation so that data written by one spec cannot influence another spec executed in a different worker, without requiring developers to add manual cy.clearLocalStorage() or cy.clearSessionStorage() calls in every beforeEach or afterEach hook.
Currently the only documented workaround is to implement those hooks manually, and no configuration flag exists to enforce the cleanup automatically in Cypress 13.x.
Is there a configuration option to enable automatic storage reset for parallel execution? If not, what is the recommended pattern to enforce storage isolation across workers without duplicating hook code? Does the Cypress roadmap include a planned feature or flag to address this behavior?
31775 reputation · 09 Sept 2024, 16:15 UTC
localStorage/sessionStorage written by one worker can never leak into another. Cross‑worker contamination is structurally impossible.testIsolation: true (the default) resets the entire browser context — storage, cookies, page — before every spec file and before every test. If you still want a single global hook for custom cleanup, put it in cypress/support/e2e.js:
// cypress/support/e2e.js
beforeEach(() => {
// runs once per test across every spec
// cy.clearLocalStorage() is redundant but harmless here
});
The question assumes storage persists across parallel workers. In Dashboard‑orchestrated parallelism (cypress run --parallel --record), each worker gets its own isolated browser profile. Within a single worker, testIsolation clears storage between specs and between tests. The only way to see leakage is:
testIsolation: false in cypress.config.js.testIsolation is not overridden:
// cypress.config.js
export default defineConfig({
e2e: { testIsolation: true } // default, can be omitted
});
// cypress/e2e/spec-a.cy.js
it('writes sentinel', () => {
localStorage.setItem('worker-token', 'spec-a');
});
// cypress/e2e/spec-b.cy.js
it('asserts isolation', () => {
expect(localStorage.getItem('worker-token')).to.be.null;
});
npx cypress run --parallel --record (requires Dashboard project). Spec B will pass — the key is absent.Use cy.session() for authenticated sessions or expensive setup. It coexists with test isolation and is the supported way to cache state across tests within a spec.
Are you using Cypress Cloud (--parallel --record) or a local parallel runner (e.g., cypress-parallel, xargs, custom script) that launches multiple browsers on the same machine? If the latter, you do need explicit cleanup in the support file because the browser profile is shared.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.