0
0
Cypresstesting~20 mins

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

Choose your learning style9 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.