Bird
Raised Fist0
Cypresstesting~20 mins

beforeEach and afterEach hooks in Cypress - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Hook Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of beforeEach and afterEach hooks in Cypress
Consider the following Cypress test code. What will be the order of console logs when the tests run?
Cypress
describe('Test Suite', () => {
  beforeEach(() => {
    cy.log('beforeEach hook');
  });

  afterEach(() => {
    cy.log('afterEach hook');
  });

  it('Test 1', () => {
    cy.log('Test 1 body');
  });

  it('Test 2', () => {
    cy.log('Test 2 body');
  });
});
A2,1,3,5,4,6
B1,2,3,4,5,6
C1,3,2,4,6,5
D3,1,2,6,4,5
Attempts:
2 left
💡 Hint
Remember that beforeEach runs before each test and afterEach runs after each test.
❓ assertion
intermediate
1:30remaining
Correct assertion placement with beforeEach and afterEach
Where should you place assertions to verify a UI element is visible before each test and cleaned up after each test?
APlace visibility assertion inside beforeEach and cleanup assertion inside afterEach
BPlace all assertions inside the first test only
CPlace visibility assertion inside afterEach and cleanup assertion inside beforeEach
DPlace all assertions inside afterEach only
Attempts:
2 left
💡 Hint
Think about when the UI element should be visible and when it should be cleaned up.
🔧 Debug
advanced
2:00remaining
Debugging unexpected test order with beforeEach and afterEach
You notice that your afterEach hook is not running after the first test but runs after the second test. What is the most likely cause?
AThe first test contains an asynchronous command without returning or awaiting it
BThe beforeEach hook is missing
CThe afterEach hook is defined inside the first test block
DThe test suite has only one test
Attempts:
2 left
💡 Hint
Consider how Cypress handles asynchronous commands and test completion.
❓ framework
advanced
1:30remaining
Using beforeEach and afterEach with Cypress custom commands
You want to log in a user before each test and log out after each test using custom Cypress commands cy.login() and cy.logout(). Which setup is correct?
Cypress
describe('User tests', () => {
  beforeEach(() => {
    cy.login();
  });

  afterEach(() => {
    cy.logout();
  });

  it('checks dashboard', () => {
    cy.get('#dashboard').should('be.visible');
  });
});
ALogin should be in afterEach and logout in beforeEach
BbeforeEach and afterEach hooks cannot use custom commands
CBoth login and logout should be inside each test, not hooks
DThis setup is correct and will log in before and log out after each test
Attempts:
2 left
💡 Hint
Think about the test lifecycle and when setup and teardown should happen.
🧠 Conceptual
expert
2:30remaining
Impact of nested beforeEach and afterEach hooks in Cypress
Given nested describe blocks with beforeEach and afterEach hooks, what is the order of execution for hooks and tests?
Cypress
describe('Outer Suite', () => {
  beforeEach(() => cy.log('Outer beforeEach'));
  afterEach(() => cy.log('Outer afterEach'));

  describe('Inner Suite', () => {
    beforeEach(() => cy.log('Inner beforeEach'));
    afterEach(() => cy.log('Inner afterEach'));

    it('Inner test', () => cy.log('Test body'));
  });
});
AOuter beforeEach, Test body, Inner beforeEach, Outer afterEach, Inner afterEach
BInner beforeEach, Outer beforeEach, Test body, Outer afterEach, Inner afterEach
COuter beforeEach, Inner beforeEach, Test body, Inner afterEach, Outer afterEach
DTest body, Outer beforeEach, Inner beforeEach, Outer afterEach, Inner afterEach
Attempts:
2 left
💡 Hint
Hooks in outer describe run before hooks in inner describe for beforeEach, and reverse for afterEach.

Practice

(1/5)
1. What is the main purpose of the beforeEach hook in Cypress testing?
easy
A. To run code before every test to set up the test environment
B. To run code after every test to clean up the test environment
C. To skip a test conditionally
D. To group multiple tests together

Solution

  1. Step 1: Understand the role of beforeEach

    The beforeEach hook runs before each test to prepare the environment or state needed for the test.
  2. Step 2: Differentiate from afterEach

    Unlike afterEach, which runs after tests to clean up, beforeEach sets up conditions before tests.
  3. Final Answer:

    To run code before every test to set up the test environment -> Option A
  4. Quick Check:

    beforeEach = setup before tests [OK]
Hint: Remember: beforeEach runs before tests, afterEach runs after [OK]
Common Mistakes:
  • Confusing beforeEach with afterEach
  • Thinking beforeEach skips tests
  • Assuming beforeEach groups tests
2. Which of the following is the correct syntax to use afterEach in a Cypress test file?
easy
A. afterEach { cy.visit('/home') }
B. afterEach(() => { cy.visit('/home') })
C. afterEach: () => cy.visit('/home')
D. afterEach => { cy.visit('/home') }

Solution

  1. Step 1: Recall correct function syntax in Cypress

    Cypress hooks use function calls with arrow functions inside parentheses, like afterEach(() => { ... }).
  2. Step 2: Identify syntax errors in other options

    Options B, C, and D miss parentheses or use incorrect arrow function syntax.
  3. Final Answer:

    afterEach(() => { cy.visit('/home') }) -> Option B
  4. Quick Check:

    Hook syntax = function call with arrow function [OK]
Hint: Hooks need parentheses and arrow functions: hook(() => {}) [OK]
Common Mistakes:
  • Omitting parentheses after hook name
  • Using curly braces without parentheses
  • Incorrect arrow function syntax
3. Consider this Cypress test code:
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?
medium
A. Once before all tests
B. Twice, once after each test
C. Twice, once before each test
D. Once after all tests

Solution

  1. Step 1: Understand beforeEach behavior

    The beforeEach hook runs before every individual test inside the describe block.
  2. Step 2: Count the tests and hook calls

    There are two tests, so cy.visit('/login') runs twice, once before each test.
  3. Final Answer:

    Twice, once before each test -> Option C
  4. Quick Check:

    beforeEach runs before every test [OK]
Hint: Count tests; beforeEach runs before each one [OK]
Common Mistakes:
  • Thinking beforeEach runs only once
  • Confusing beforeEach with afterEach
  • Assuming hooks run after tests
4. You wrote this Cypress test:
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?
medium
A. Because cy.clearCookies is missing parentheses to call the function
B. Because afterEach only runs before tests
C. Because cy.clearCookies is not a valid Cypress command
D. Because beforeEach overrides afterEach

Solution

  1. Step 1: Check the afterEach hook code

    The code uses cy.clearCookies without parentheses, so the function is not called.
  2. Step 2: Understand function call syntax

    To execute the command, parentheses are required: cy.clearCookies().
  3. Final Answer:

    Because cy.clearCookies is missing parentheses to call the function -> Option A
  4. Quick Check:

    Functions need () to run [OK]
Hint: Always add () to call Cypress commands [OK]
Common Mistakes:
  • Forgetting parentheses on Cypress commands
  • Confusing beforeEach and afterEach roles
  • Assuming afterEach runs before tests
5. You want to test a form that requires logging in before each test and clearing local storage after each test. Which is the best way to organize your Cypress hooks?
hard
A. Use only beforeEach for both login and clearing local storage
B. Use afterEach to log in and beforeEach to clear local storage
C. Use only afterEach for both login and clearing local storage
D. Use beforeEach to log in and afterEach to clear local storage

Solution

  1. Step 1: Identify setup and cleanup tasks

    Logging in is setup needed before each test; clearing local storage is cleanup after each test.
  2. Step 2: Assign tasks to correct hooks

    Use beforeEach for setup (login) and afterEach for cleanup (clear local storage).
  3. Final Answer:

    Use beforeEach to log in and afterEach to clear local storage -> Option D
  4. Quick Check:

    Setup beforeEach, cleanup afterEach [OK]
Hint: Setup in beforeEach, cleanup in afterEach [OK]
Common Mistakes:
  • Mixing setup and cleanup in wrong hooks
  • Using only one hook for both tasks
  • Not cleaning up after tests
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard