Bird
0
0

Consider this Cypress test code:

medium📝 Predict Output Q13 of 15
Cypress - Writing Tests
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?
AOnce before all tests
BTwice, once after each test
CTwice, once before each test
DOnce after all tests
Step-by-Step Solution
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]
Quick Trick: Count tests; beforeEach runs before each one [OK]
Common Mistakes:
  • Thinking beforeEach runs only once
  • Confusing beforeEach with afterEach
  • Assuming hooks run after tests

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes