Bird
0
0

Which of the following is the correct syntax to run a cleanup function after each test in Cypress?

easy📝 Syntax Q3 of 15
Cypress - Test Organization and Patterns
Which of the following is the correct syntax to run a cleanup function after each test in Cypress?
Aafter(() => { cy.task('cleanup') })
BafterEach(() => { cy.task('cleanup') })
CbeforeEach(() => { cy.task('cleanup') })
Dcleanup(() => { cy.task('cleanup') })
Step-by-Step Solution
Solution:
  1. Step 1: Recall Cypress hook syntax

    The afterEach hook runs after every test, and the syntax is afterEach(() => { ... }).
  2. Step 2: Verify other options

    after runs once after all tests, beforeEach runs before each test, and cleanup is not a valid hook.
  3. Final Answer:

    afterEach(() => { cy.task('cleanup') }) -> Option B
  4. Quick Check:

    Cleanup syntax = afterEach [OK]
Quick Trick: Use afterEach hook syntax for post-test cleanup [OK]
Common Mistakes:
  • Confusing after and afterEach hooks
  • Using invalid hook names like cleanup
  • Placing cleanup in beforeEach instead

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes