What if you never had to repeat boring setup steps again in your tests?
Why beforeEach and afterEach hooks in Cypress? - Purpose & Use Cases
Imagine testing a website where you must log in before every test and log out after. Doing this by hand means repeating the same steps over and over for each test case.
Manually repeating setup and cleanup steps wastes time and causes mistakes. You might forget to log out or reset data, leading to wrong test results and frustration.
Using beforeEach and afterEach hooks lets you run setup code before every test and cleanup code after every test automatically. This keeps tests clean and reliable without repeating yourself.
it('test 1', () => { cy.login() // test steps cy.logout() }) it('test 2', () => { cy.login() // test steps cy.logout() })
beforeEach(() => {
cy.login()
})
afterEach(() => {
cy.logout()
})
it('test 1', () => {
// test steps
})
it('test 2', () => {
// test steps
})This makes your tests faster to write, easier to read, and less likely to break because setup and cleanup happen automatically.
When testing an online store, you can log in once before each test and clear the shopping cart after each test without repeating those steps inside every test case.
Manual repetition of setup and cleanup is slow and error-prone.
beforeEach and afterEach hooks automate these steps for every test.
This leads to cleaner, more reliable, and easier-to-maintain tests.
Practice
beforeEach hook in Cypress testing?Solution
Step 1: Understand the role of
ThebeforeEachbeforeEachhook runs before each test to prepare the environment or state needed for the test.Step 2: Differentiate from
UnlikeafterEachafterEach, which runs after tests to clean up,beforeEachsets up conditions before tests.Final Answer:
To run code before every test to set up the test environment -> Option AQuick Check:
beforeEach= setup before tests [OK]
- Confusing beforeEach with afterEach
- Thinking beforeEach skips tests
- Assuming beforeEach groups tests
afterEach in a Cypress test file?Solution
Step 1: Recall correct function syntax in Cypress
Cypress hooks use function calls with arrow functions inside parentheses, likeafterEach(() => { ... }).Step 2: Identify syntax errors in other options
Options B, C, and D miss parentheses or use incorrect arrow function syntax.Final Answer:
afterEach(() => { cy.visit('/home') }) -> Option BQuick Check:
Hook syntax = function call with arrow function [OK]
- Omitting parentheses after hook name
- Using curly braces without parentheses
- Incorrect arrow function syntax
describe('My Tests', () => {
beforeEach(() => {
cy.visit('/login')
})
it('Test 1', () => {
cy.get('#username').type('user1')
})
it('Test 2', () => {
cy.get('#username').type('user2')
})
})How many times will
cy.visit('/login') run when these tests execute?Solution
Step 1: Understand
ThebeforeEachbehaviorbeforeEachhook runs before every individual test inside thedescribeblock.Step 2: Count the tests and hook calls
There are two tests, socy.visit('/login')runs twice, once before each test.Final Answer:
Twice, once before each test -> Option CQuick Check:
beforeEach runs before every test [OK]
- Thinking beforeEach runs only once
- Confusing beforeEach with afterEach
- Assuming hooks run after tests
describe('Sample', () => {
beforeEach(() => {
cy.visit('/dashboard')
})
afterEach(() => {
cy.clearCookies
})
it('checks title', () => {
cy.title().should('include', 'Dashboard')
})
})Why does
cy.clearCookies not run after the test?Solution
Step 1: Check the
The code usesafterEachhook codecy.clearCookieswithout parentheses, so the function is not called.Step 2: Understand function call syntax
To execute the command, parentheses are required:cy.clearCookies().Final Answer:
Becausecy.clearCookiesis missing parentheses to call the function -> Option AQuick Check:
Functions need () to run [OK]
- Forgetting parentheses on Cypress commands
- Confusing beforeEach and afterEach roles
- Assuming afterEach runs before tests
Solution
Step 1: Identify setup and cleanup tasks
Logging in is setup needed before each test; clearing local storage is cleanup after each test.Step 2: Assign tasks to correct hooks
UsebeforeEachfor setup (login) andafterEachfor cleanup (clear local storage).Final Answer:
UsebeforeEachto log in andafterEachto clear local storage -> Option DQuick Check:
Setup beforeEach, cleanup afterEach [OK]
- Mixing setup and cleanup in wrong hooks
- Using only one hook for both tasks
- Not cleaning up after tests
Ready to prove it?
Pass the test and this pattern moves unknown
