What if you never had to repeat boring setup steps in your tests again?
Why before and after hooks in Cypress? - Purpose & Use Cases
Imagine testing a website manually where you have to log in before every test and log out after each one. You repeat these steps again and again for every test case.
This manual repetition wastes time and causes mistakes. You might forget to log out or log in properly, leading to inconsistent test results and frustration.
Before and after hooks in Cypress let you run setup code once before tests and cleanup code after tests automatically. This saves time and keeps tests clean and reliable.
it('test 1', () => { cy.login(); /* test steps */ cy.logout(); }); it('test 2', () => { cy.login(); /* test steps */ cy.logout(); });
before(() => { cy.login(); });
after(() => { cy.logout(); });
it('test 1', () => { /* test steps */ });
it('test 2', () => { /* test steps */ });You can write cleaner tests that run faster and are easier to maintain by automating setup and cleanup steps.
In a shopping website test suite, you log in once before all tests and log out after all tests, so each test focuses only on checking product features without repeating login steps.
Manual repetition of setup and cleanup is slow and error-prone.
Before and after hooks automate these steps for all tests.
This leads to faster, cleaner, and more reliable test suites.
Practice
before and after hooks in Cypress testing?Solution
Step 1: Understand the role of hooks
Hooks likebeforeandafterrun code automatically before or after tests.Step 2: Identify their scope
beforeandafterrun once per test suite, ideal for setup and cleanup.Final Answer:
To run setup and cleanup code once per test suite -> Option DQuick Check:
Hooks run code around tests = D [OK]
- Confusing before with beforeEach
- Thinking hooks run inside tests
- Using hooks to skip tests
Solution
Step 1: Identify the hook that runs once before all tests
beforeruns once before the entire suite;beforeEachruns before each test.Step 2: Match the syntax
before(() => { cy.visit('/') })is the correct syntax to run once before all tests.Final Answer:
before(() => { cy.visit('/') }) -> Option AQuick Check:
Run once before all tests = before() = A [OK]
- Using beforeEach instead of before for once-per-suite setup
- Confusing after with before
- Incorrect arrow function syntax
describe('My Suite', () => {
before(() => {
cy.log('Setup once')
})
beforeEach(() => {
cy.log('Setup before each test')
})
it('Test 1', () => {
cy.log('Running Test 1')
})
it('Test 2', () => {
cy.log('Running Test 2')
})
})What is the order of logs printed during test execution?
Solution
Step 1: Understand hook execution order
beforeruns once before all tests,beforeEachruns before each test.Step 2: Trace the logs
Logs print in this order: 'Setup once' (before all), then for Test 1: 'Setup before each test', 'Running Test 1', then for Test 2: 'Setup before each test', 'Running Test 2'.Final Answer:
Setup once, Setup before each test, Running Test 1, Setup before each test, Running Test 2 -> Option BQuick Check:
before runs once, beforeEach runs before each test = A [OK]
- Thinking beforeEach runs only once
- Mixing order of logs
- Ignoring before hook
describe('Test Suite', () => {
beforeEach(() => {
cy.visit('/home')
})
after(() => {
cy.clearCookies()
})
it('Test A', () => {
cy.get('#btn').click()
})
it('Test B', () => {
cy.get('#btn').click()
})
})But the
cy.clearCookies() is not running after each test as expected. What is the problem?Solution
Step 1: Understand the difference between after and afterEach
afterruns once after all tests;afterEachruns after each test.Step 2: Identify the problem
afterruns once after all tests, not after each test.Final Answer:
afterruns once after all tests, not after each test -> Option AQuick Check:
after runs once per suite = A [OK]
- Using after instead of afterEach for per-test cleanup
- Expecting after to run multiple times
- Manually clearing cookies inside tests
Solution
Step 1: Assign setup and cleanup hooks
Setup runs once before all tests usingbefore, cleanup runs once after all tests usingafter.Step 2: Assign login before each test
Login runs before each test usingbeforeEach.Step 3: Verify correct order and usage
before(() => { cy.setup() }); after(() => { cy.cleanup() }); beforeEach(() => { cy.login() }); correctly usesbefore,after, andbeforeEachfor these steps.Final Answer:
before(() => { cy.setup() }); after(() => { cy.cleanup() }); beforeEach(() => { cy.login() }); -> Option CQuick Check:
Setup once before, cleanup once after, login before each = B [OK]
- Mixing beforeEach with once-per-suite hooks
- Placing login in before or after hooks incorrectly
- Swapping setup and cleanup hooks
Ready to prove it?
Pass the test and this pattern moves unknown
