Auto‑clear storage vs manual cy.clearLocalStorage() for Cypress memory‑leak mitigation
0 reputation · 09 Dec 2021, 03:55 UTC
0 reputation · 09 Dec 2021, 03:55 UTC
Goal: Choose a strategy that prevents localStorage/sessionStorage accumulation in long Cypress runs while keeping test execution time low and preserving isolation.
Constraint: Cypress does not clear storage automatically; manual cy.clearLocalStorage() in afterEach removes data but adds teardown overhead, whereas navigating to about:blank in beforeEach clears storage without extra commands but reloads the page and may disturb cookies or authentication state.
Uncertainty: Whether the framework should provide a built‑in option to auto‑clear storage between tests, leaving users to weigh the performance impact of explicit clearing against the risk of memory growth and possible iframe‑specific storage.
Should Cypress add a configuration flag to auto‑clear localStorage and sessionStorage after each test? Does the overhead of cy.clearLocalStorage() outweigh the benefit of avoiding manual cleanup? How does the blank‑page reset approach compare in speed and state isolation for tests that rely on persisted cookies?
35775 reputation · 09 Dec 2021, 07:30 UTC
If you're on Cypress 12 or later, you probably don't need any of the three options you're weighing — and you don't need a new auto-clear flag either. Since v12, testIsolation defaults to true, which means Cypress already clears localStorage, sessionStorage, and cookies before every test. The premise that "Cypress does not clear storage automatically" describes pre-v12 behavior (or a config where testIsolation was explicitly disabled). The manual afterEach(() => cy.clearLocalStorage()) hook and the about:blank navigation trick were workarounds for that older behavior.
This is the fastest and most predictable option. The reset happens at the browser level between tests, not as a Cypress command, so it doesn't add to your command queue, doesn't appear in the log, and clears cookies and both storage types together. There is nothing to configure unless someone turned it off.
Under default isolation this is redundant — each call is a real command that visits the origin and executes, so across hundreds of tests the overhead is small but measurable, and it buys you nothing the framework doesn't already do. Keep it only if testIsolation is false or you're pinned to Cypress < 12. One caveat: cy.clearLocalStorage() with no arguments clears all keys, so if any test seeds storage in beforeEach, ordering mistakes will break it.
This was the standard pre-v12 trick. It clears origin-scoped storage, but it forces a page reload, doesn't cleanly reset cookies, and can destroy authentication state mid-suite. Compared with the built-in reset it is slower and less predictable. There is no reason to prefer it on a current Cypress version.
Storage accumulation is rarely the real driver of memory growth in long Cypress runs. The command log, DOM snapshots, and screenshots retained per test typically dominate browser memory pressure. Clearing localStorage alone is unlikely to fix an out-of-memory crash — if that's your actual symptom, look at run length, numTestsKeptInMemory, and screenshot/video settings instead.
For tests that rely on a persisted SSO session, the intended mechanism is cy.session(). It caches and restores authentication state quickly while storage isolation still prevents cross-test leakage — so you get isolation and fast login without disabling testIsolation globally.
Storage belongs to origins, not frames. A same-origin iframe shares the parent origin's storage and is covered by the same isolation reset. Cross-origin iframe storage isn't reachable via cy.clearLocalStorage() at all, so no manual strategy fully addresses it either.
package.json and search cypress.config.js/cypress.config.ts for testIsolation. On v12+ with no override, storage is already cleared between tests.afterEach hook.cy.session() restores auth across tests while isolation stays enabled.One diagnostic detail would change this recommendation: your Cypress version. If you're on v11 or earlier and can't upgrade, then the manual afterEach clear is the better of your two workarounds — it's more explicit and less disruptive to cookies than the about:blank reload.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.