Bird
0
0

Why might tests fail if you use cy.visit() only once in a before() hook instead of beforeEach()?

medium📝 Debug Q7 of 15
Cypress - Test Organization and Patterns
Why might tests fail if you use cy.visit() only once in a before() hook instead of beforeEach()?
ABecause the page state is not reset before each test
BBecause before() runs after all tests
CBecause cy.visit() cannot be used in before()
DBecause before() runs multiple times unnecessarily
Step-by-Step Solution
Solution:
  1. Step 1: Understand before() vs beforeEach()

    before() runs once before all tests, beforeEach() runs before each test.
  2. Step 2: Identify impact on page state

    Using before() means page is visited once, so tests share state and may fail.
  3. Final Answer:

    Because the page state is not reset before each test -> Option A
  4. Quick Check:

    Use beforeEach() to reset state per test [OK]
Quick Trick: Use beforeEach() to reset state before every test [OK]
Common Mistakes:
  • Thinking before() runs after tests
  • Believing cy.visit() is invalid in before()
  • Assuming before() runs multiple times

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes