Bird
0
0

You want to run a setup step once before all tests, a cleanup step once after all tests, and also run a login step before each test in Cypress. Which code snippet correctly implements this?

hard📝 Application Q15 of 15
Cypress - Writing Tests
You want to run a setup step once before all tests, a cleanup step once after all tests, and also run a login step before each test in Cypress. Which code snippet correctly implements this?
AbeforeEach(() => { cy.setup() }); afterEach(() => { cy.cleanup() }); before(() => { cy.login() });
Bbefore(() => { cy.login() }); afterEach(() => { cy.cleanup() }); beforeEach(() => { cy.setup() });
Cbefore(() => { cy.setup() }); after(() => { cy.cleanup() }); beforeEach(() => { cy.login() });
Dafter(() => { cy.setup() }); before(() => { cy.cleanup() }); afterEach(() => { cy.login() });
Step-by-Step Solution
Solution:
  1. Step 1: Assign setup and cleanup hooks

    Setup runs once before all tests using before, cleanup runs once after all tests using after.
  2. Step 2: Assign login before each test

    Login runs before each test using beforeEach.
  3. Step 3: Verify correct order and usage

    before(() => { cy.setup() }); after(() => { cy.cleanup() }); beforeEach(() => { cy.login() }); correctly uses before, after, and beforeEach for these steps.
  4. Final Answer:

    before(() => { cy.setup() }); after(() => { cy.cleanup() }); beforeEach(() => { cy.login() }); -> Option C
  5. Quick Check:

    Setup once before, cleanup once after, login before each = B [OK]
Quick Trick: Use before for setup, after for cleanup, beforeEach for per-test login [OK]
Common Mistakes:
  • Mixing beforeEach with once-per-suite hooks
  • Placing login in before or after hooks incorrectly
  • Swapping setup and cleanup hooks

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes