How does Cypress isolate tests from each other?
Since Cypress 12 the testIsolation option is on by default and clears the browser before every test. Cypress navigates to about:blank and clears cookies, localStorage and sessionStorage, so a test does not depend on run order and a retry starts clean. Data on the server stays, so each test prepares its own state and cleans up or uniquifies data. Turning isolation off is an exception for a long scenario.
Cypress · Sessions and isolation
Test B works only if test A ran before it. Why is that not allowed and what does Cypress do?
Test A logs a user in and opens the dashboard, test B just calls cy.get('[data-cy=balance]') and expects everything to be ready. Run on its own or with .only, B fails. To prevent that, Cypress clears the browser before every test. The lesson covers what exactly is cleared, what is not, and what follows for tests.
How it works and why. Since Cypress 12 the testIsolation option is on by default. Before every test Cypress navigates to about:blank and also clears cookies, localStorage and sessionStorage for every origin. The goal is simple: a test does not depend on the run order, it can be run on its own, and a retry after a failure starts from a clean state. The price: every test prepares its own state, that is, opens the page and, if needed, logs in.
What is cleared between tests
Example: a test that lives on the leftovers of another.
Login only in the first test
The second test knows nothing about the login: after cookies are cleared it is redirected to the sign-in page and cy.get will not find the balance.
Every test prepares its own state
Shared preparation in beforeEach: login (preferably through cy.session) and cy.visit. The test can be run on its own.
describe('Dashboard', () => {
beforeEach(() => {
cy.login('user'); // cy.session inside
cy.visit('/dashboard');
});
it('shows the balance', () => {
cy.get('[data-cy=balance]').should('be.visible');
});
it('opens the transaction history', () => {
cy.contains('a', 'History').click();
cy.url().should('include', '/history');
});
});
Here no test depends on its neighbour: the hook prepares the same start and every it checks its own thing. The cy.login command is described in the lesson about cy.session.
Limits and alternatives. The clearing concerns the browser. Data a test created on the server (an order, a user in the database) stays, so it needs separate cleanup or unique data. Isolation can be switched off: testIsolation: false in the config or for a single describe, for example describe('flow', { testIsolation: false }, …). Then the page and state carry over between tests, which speeds up a long scenario, but tests become order-dependent and a retry does not start from a clean state. It is an exception, not a habit.
Isolation out of the box is different: every test gets a new BrowserContext, a separate profile like incognito. Nothing is erased, the context is created anew in milliseconds.
Common mistakes.
- Logging in during one test and trying to “reuse” it in the next: after cookies are cleared the user is logged out.
- Relying on data created by a previous test: it stayed on the server, but the run order is not guaranteed.
- Turning
testIsolationoff everywhere for speed: tests become brittle and cannot be run alone. - Assuming the clearing also cleans the database: Cypress does not reset application data.
Check yourself
Test A creates an order through the UI, test B checks that the list has exactly one order, and is green. Is that reliable?
Answer
No. Isolation clears the browser, but the order from A stayed on the server, so B passes only by accident of order. Alone, B would fail. Data must be prepared in the test itself or cleaned up.
When might testIsolation: false be needed and what is the price?
Answer
For a long end-to-end scenario split into steps, when you do not want to reopen the page each time. The price: tests depend on order, one failure breaks the next ones and a retry does not restore a clean state.
testIsolation. So tests are independent and each prepares its own state. Middle/Senior: I explain the difference from Playwright like this: there is a new context per test, here a cleanup in the same browser. The cleanup does not touch the server, so I isolate test data separately and move login into cy.session so preparing state does not cost a full sign-in every time.