0
0
Cypresstesting~5 mins

before and after hooks in Cypress - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of before hooks in Cypress tests?

before hooks run once before all tests in a block. They set up things like visiting a page or logging in, so tests start in the right state.

Click to reveal answer
beginner
How does the after hook help in Cypress testing?

after hooks run once after all tests finish. They clean up or reset things, like logging out or clearing data, to keep tests independent.

Click to reveal answer
intermediate
What is the difference between before and beforeEach hooks?

before runs once before all tests, while beforeEach runs before every single test. Use beforeEach to reset state before each test.

Click to reveal answer
beginner
Show a simple example of using before and after hooks in Cypress.
describe('My Test Suite', () => {
  before(() => {
    cy.visit('/login')
  })

  after(() => {
    cy.clearCookies()
  })

  it('checks page title', () => {
    cy.title().should('include', 'Login')
  })
})
Click to reveal answer
intermediate
Why should tests avoid relying on afterEach hooks for cleanup?

Because it can lead to tests depending on previous outcomes. It's safer to use beforeEach to reset state before each test or design tests to be independent.

Click to reveal answer
When does the before hook run in Cypress?
AAfter each test
BBefore each test
COnce before all tests in a describe block
DOnce after all tests
Which hook runs after all tests finish in a Cypress test suite?
AafterEach
Bafter
CbeforeEach
Dbefore
What is a good use case for beforeEach in Cypress?
ATo reset application state before each test
BTo skip tests
CTo clean up after all tests
DTo run setup once before all tests
What happens if a test fails before the afterEach hook runs?
AThe <code>afterEach</code> hook still runs
BThe test automatically passes
CThe <code>afterEach</code> hook might not run, causing leftover state
DThe test suite stops immediately
Which hook is best to visit a page once before all tests?
Aafter
BafterEach
CbeforeEach
Dbefore
Explain how before and after hooks help organize Cypress tests.
Think about setup and cleanup tasks.
You got /5 concepts.
    Describe the difference between beforeEach and before hooks with examples.
    Focus on how often each hook runs.
    You got /4 concepts.