Bird
0
0

You wrote this code to preserve cookies but the second test fails because the cookie is missing:

medium📝 Debug Q14 of 15
Cypress - Authentication and Sessions
You wrote this code to preserve cookies but the second test fails because the cookie is missing:
beforeEach(() => {
  Cypress.Cookies.preserveOnce('auth_token')
})

it('Login test', () => {
  cy.setCookie('auth_token', 'xyz789')
})

it('Dashboard test', () => {
  cy.getCookie('auth_token').should('exist')
})

What is the likely cause of the failure?
AThe cookie is set after the beforeEach hook runs, so it is not preserved
BThe cookie name is misspelled in preserveOnce
CCypress cannot preserve cookies between tests
DThe assertion syntax is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Understand when preserveOnce runs

    The beforeEach hook runs before each test, so it preserves cookies existing before the test starts.
  2. Step 2: Analyze cookie setting timing

    The cookie is set inside the first test, after beforeEach runs, so it is not preserved for the next test.
  3. Final Answer:

    The cookie is set after the beforeEach hook runs, so it is not preserved -> Option A
  4. Quick Check:

    PreserveOnce must run after cookie set [OK]
Quick Trick: Set cookies before preserveOnce runs to keep them [OK]
Common Mistakes:
  • Thinking preserveOnce preserves cookies set during the test
  • Assuming Cypress can't preserve cookies
  • Blaming assertion syntax instead of timing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes