Bird
0
0

Find the issue in this Cypress test code:

medium📝 Debug Q7 of 15
Cypress - Authentication and Sessions
Find the issue in this Cypress test code:
cy.clearLocalStorage()
cy.window().then(win => {
  const data = win.localStorage.getItem('data')
  expect(data).to.equal('test')
})
AgetItem should be called with cy.getItem()
BlocalStorage cannot be accessed inside cy.window()
Cexpect syntax is incorrect; should use should('equal')
DclearLocalStorage is asynchronous; expect runs before clearing completes
Step-by-Step Solution
Solution:
  1. Step 1: Understand asynchronous behavior

    cy.clearLocalStorage() is async; next commands run before clearing finishes.
  2. Step 2: Identify timing issue

    Expect runs immediately, so data may not be cleared yet, causing test failure.
  3. Final Answer:

    clearLocalStorage is asynchronous; expect runs before clearing completes -> Option D
  4. Quick Check:

    Wait for async commands before assertions [OK]
Quick Trick: Chain .then() after clearLocalStorage before assertions [OK]
Common Mistakes:
  • Ignoring async nature of clearLocalStorage
  • Using wrong method getItem on cy object
  • Misusing expect syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes