0
0
Cypresstesting~20 mins

before and after 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 before and after hooks in Cypress

What will be the output order of console logs when running this Cypress test?

Cypress
describe('Test hooks order', () => {
  before(() => {
    cy.log('before all')
  })

  beforeEach(() => {
    cy.log('before each')
  })

  it('test 1', () => {
    cy.log('test 1')
  })

  it('test 2', () => {
    cy.log('test 2')
  })

  afterEach(() => {
    cy.log('after each')
  })

  after(() => {
    cy.log('after all')
  })
})
Abefore all, before each, test 1, before each, test 2, after each, after each, after all
Bbefore each, before all, test 1, after each, before each, test 2, after each, after all
Cbefore all, test 1, before each, after each, before each, test 2, after each, after all
Dbefore all, before each, test 1, after each, before each, test 2, after each, after all
Attempts:
2 left
💡 Hint

Remember the order: before runs once before all tests, beforeEach runs before each test, afterEach runs after each test, and after runs once after all tests.

assertion
intermediate
2:00remaining
Correct assertion to verify after hook effect

You want to verify that a cleanup function in an after hook resets a global variable count to zero after all tests run. Which assertion correctly checks this?

Cypress
let count = 0;
describe('Count tests', () => {
  after(() => {
    count = 0;
  });

  it('increments count', () => {
    count += 1;
  });

  it('increments count again', () => {
    count += 2;
  });
});
Aexpect(count).to.equal(0); // placed inside after hook
Bexpect(count).to.equal(3); // placed inside last test
Cexpect(count).to.equal(0); // placed after describe block
Dexpect(count).to.equal(3); // placed inside after hook
Attempts:
2 left
💡 Hint

The after hook runs after all tests, so assertions about cleanup should be inside it.

🔧 Debug
advanced
2:00remaining
Debug why afterEach hook is not running

Given this Cypress test, why is the afterEach hook not running after the tests?

Cypress
describe('Sample tests', () => {
  afterEach(() => {
    cy.log('Cleaning up');
  });

  it('test A', () => {
    cy.log('Running test A');
  });

  context('Nested context', () => {
    it('test B', () => {
      cy.log('Running test B');
    });
  });
});
AThe afterEach hook must be declared inside each it block to run.
BThe afterEach hook is missing a call to done(), so it never completes.
CThe afterEach hook only runs for tests in the same describe block, so it does not run after test B inside nested context.
DThe afterEach hook is asynchronous and needs await to run properly.
Attempts:
2 left
💡 Hint

Consider how hooks apply to nested blocks in Cypress.

🧠 Conceptual
advanced
2:00remaining
Purpose of before and after hooks in test suites

Which statement best describes the purpose of before and after hooks in Cypress test suites?

AThey run before and after each individual test to isolate test data.
BThey run setup and cleanup code once before and after all tests in a suite, ensuring consistent test environment.
CThey replace the need for assertions by automatically verifying test outcomes.
DThey are used to skip tests conditionally based on environment variables.
Attempts:
2 left
💡 Hint

Think about when before and after hooks run relative to tests.

framework
expert
3:00remaining
Identify correct Cypress hook usage for database reset

You want to reset the test database only once before all tests run and close the database connection only once after all tests finish. Which hook pair correctly implements this in Cypress?

AUse <code>before()</code> to reset database and <code>after()</code> to close connection.
BUse <code>beforeEach()</code> to reset database and <code>afterEach()</code> to close connection.
CUse <code>before()</code> to reset database and <code>afterEach()</code> to close connection.
DUse <code>beforeEach()</code> to reset database and <code>after()</code> to close connection.
Attempts:
2 left
💡 Hint

Consider how often you want each action to run.