Cache Authenticated Sessions in Cypress Without Repeating Login Flows
Use cy.session() to cache cookies and localStorage across Cypress specs, speeding up authenticated tests while understanding the isolation trade-offs.
26 Aug 2026, 10:23 UTC

Repeated logins slow down large Cypress suites
If your end-to-end suite has dozens of specs that visit authenticated pages, each spec may start by filling in a username, submitting a password, and waiting for a redirect. That login flow adds seconds to every test file. In continuous integration, those seconds multiply across parallel runners and retries. The result is slower feedback and more chances for a flaky login selector to fail an otherwise healthy test.
Cypress provides cy.session() to cache the result of an authentication routine. After the first login in a run, Cypress stores the cookies and localStorage produced by that login and restores them at the start of later tests. Specs that need an authenticated browser can skip the login page entirely.
Set up a cached session
Call cy.session() in a beforeEach hook or at the top of a spec. The first argument is a session name. The second is a function that performs the login. An optional third argument lets you validate whether the cached state is still good.
Run this command in your project root to confirm your installed version:
npx cypress version
Match the output against the documentation for your installed Cypress major version. The cy.session() signature changed between releases, so do not copy examples from a different version without checking.
Example configuration in cypress/e2e/authenticated.cy.js:
import { loginAsAdmin } from '../support/helpers';
describe('Admin dashboard', () => {
beforeEach(() => {
cy.session('admin-session', () => {
loginAsAdmin();
}, {
validate: () => {
cy.request({
url: '/api/session',
failOnStatusCode: false,
}).then((response) => {
expect(response.status).to.eq(200);
});
},
});
});
it('shows the analytics widget', () => {
cy.visit('/admin/analytics');
cy.get('[data-cy=analytics-widget]').should('be.visible');
});
});
Place this file in your Cypress project's e2e folder. You need read/write access to the repository, but no production credentials should appear in the helper. Store test-only usernames and passwords in environment variables or Cypress configuration, never in committed source code.
Expected check: open the Cypress runner, run the spec, and watch the first test. The browser should complete the login flow. Run the spec again or add a second test in the same suite; the browser should skip the login page and go directly to the protected route.
What gets cached and when it refreshes
Cypress caches cookies and localStorage associated with the session name. When a later test starts, Cypress restores that storage before the page loads. If you provide a validate callback, Cypress runs it before restoring the cache. If validation fails, the login routine runs again and the cache is updated.
This works across test files within a single Cypress run. It does not persist sessions between separate CI jobs or local runs unless you configure external persistence, which Cypress does not provide out of the box.
Trade-offs: speed versus isolation
The main risk is weaker test isolation. Because multiple tests share the same cached cookies and tokens, a test that mutates server-side state can affect another test that assumes a fresh account. Cached sessions can also hide regressions in the login flow itself. If most tests never submit the login form, a broken password reset or lockout behavior might go unnoticed until someone runs the full suite without caching.
Use cached sessions for specs that do not test authentication. Keep at least one dedicated spec that exercises login, logout, and expired-token handling without cy.session().
Do not cache production credentials or real user tokens. Use test accounts created for automation, and restrict cached session usage to local machines and CI runners your team controls.
Verify the result
- Run the spec before adding
cy.session()and record the wall-clock time. - Add the session block and run the same spec again.
- Compare execution time and confirm the login page is not visited in cached tests.
- Open DevTools during a test, go to Application > Cookies and Local Storage, and confirm the expected authentication cookies are present after session restore.
If execution time does not improve, the bottleneck may be page load or API calls rather than login UI. If tests fail intermittently after caching, disable cy.session() temporarily to determine whether shared authentication state is the cause.
Closing
cy.session() is a practical way to remove repeated login overhead from Cypress suites, but it is not a default for every test. Cache authentication only for specs that treat login as setup, keep dedicated auth tests uncached, and verify behavior with the runner and DevTools before trusting the cache in CI.