What hooks does Cypress have and how do they work?
Cypress inherits its hooks from Mocha: before and after run once per block, beforeEach and afterEach around every test. Hooks of an outer describe wrap those of an inner one: beforeEach goes from outer to inner, afterEach in reverse. State is prepared in beforeEach, because afterEach does not run when the run is interrupted, and a failing hook makes Mocha skip the rest of the block.
Cypress · Practice and details
A run was stopped mid-test and the next one starts with dirty state. Why does cleanup in afterEach not save you?
The cart still has items from the previous test, and the new «empty cart» test fails. The cause is not the test itself but where the cleanup lives: it was in afterEach, and the run was interrupted. This lesson covers Cypress hooks: which ones exist, in what order they run, and where to put setup and cleanup so state does not leak between tests.
How it works and why
Cypress inherits hooks from Mocha. before runs once before all tests of a block, beforeEach before every test, afterEach after every test, after once after all of them. Hooks of an outer describe wrap those of an inner one: beforeEach goes from outer to inner, afterEach the other way. Hooks in cypress/support/e2e.ts belong to the root of every spec and apply to every test.
Where to clean up state
afterEach
Does not run if the run is stopped mid-test. The next run starts with leftovers.
beforeEach
Every test guarantees its own starting state, however the previous one ended.
This is why the Cypress team advises preparing state before a test rather than cleaning after it. There is a second reason: when a test fails you want to see the app in the state it failed in (snapshot, video, an open browser in cypress open), and afterEach would change it. Separately from hooks, testIsolation (on by default since Cypress 12) clears cookies, localStorage, sessionStorage and opens a blank page before every test. It does not touch data on the server: the test prepares that itself.
Example: before → after
Before: the cart is cleaned in afterEach, and an interrupted run leaves an item on the server.
describe('Cart', () => {
afterEach(() => {
cy.request('DELETE', '/api/cart'); // skipped on abort
});
it('adds an item', () => {
cy.login();
cy.visit('/catalog');
cy.get('[data-cy=add]').first().click();
});
});
After: the test guarantees its own starting state, independent of the previous one.
describe('Cart', () => {
beforeEach(() => {
cy.request('DELETE', '/api/cart'); // clean state BEFORE the test
cy.login();
cy.visit('/cart');
});
it('empty cart', () => {
cy.contains('Your cart is empty').should('be.visible');
});
});
Resetting through the API is faster and more stable than clicking through the UI, and the login is worth moving into cy.session so it is not repeated for every test.
Limits and alternatives
- If a hook fails, Mocha skips the remaining tests of the block: one failure in
beforeEachbecomes a dozen «skipped» tests. Keep hooks simple and reliable. - A
beforethat fills the database is a trap: withtestIsolationthe browser state resets between tests, while server data stays and tests can change it. Prepare whatever a test mutates inbeforeEach. - Playwright has the same hooks (
test.beforeEach,test.afterAll…), but setup and teardown usually live in fixtures: the code afterawait use()runs even when the test fails.
Common mistakes
- Cleanup in
afterEach: when the run is aborted the state stays and the next run starts from leftovers. - An arrow function where Mocha's
thisis needed (context, aliases viathis.alias): with=>thisis different. Use a regularfunctionfor aliases. - A global
beforeEachwith a login insupport/e2e.ts: it also runs for tests that do not need a login and adds seconds to each.
Check yourself
A test with afterEach cleanup was interrupted mid-run. What happens to the next run and how do you fix it?
Answer
afterEach did not run, so the data (an item in the cart) stays on the server and the next test starts with it. Fix: move the reset to beforeEach (ideally via the API) so each test guarantees its own starting state.
In what order do the hooks run if there is a beforeEach in support/e2e.ts, in an outer describe and in a nested one?
Answer
From the root to the deepest: first support, then the outer describe, then the nested one, and only then the test. afterEach hooks run in reverse.
before and after run once per block, beforeEach and afterEach for every test. Middle/Senior: I do setup and cleanup of state in beforeEach, because afterEach does not run when a run is aborted. I reset data through the API, move the login into cy.session, and remember testIsolation: browser state resets by itself, server state does not. In Playwright fixtures do this, and the code after use() always runs.