A. The cookie is set after the beforeEach hook runs, so it is not preserved
B. The cookie name is misspelled in preserveOnce
C. Cypress cannot preserve cookies between tests
D. The assertion syntax is incorrect
Solution
Step 1: Understand when preserveOnce runs
The beforeEach hook runs before each test, so it preserves cookies existing before the test starts.
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.
Final Answer:
The cookie is set after the beforeEach hook runs, so it is not preserved -> Option A
Quick Check:
PreserveOnce must run after cookie set [OK]
Hint: 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
5. You want to preserve multiple cookies named 'token' and 'user_id' across all tests without calling preserveOnce in every test. Which is the best way to do this in Cypress?
hard
A. Disable cookie clearing globally with Cypress.config
B. Call Cypress.Cookies.preserveOnce('token', 'user_id') in each test
C. Set cookies again in every test manually
D. Use Cypress.Cookies.defaults({ whitelist: ['token', 'user_id'] }) in support file
Solution
Step 1: Understand preserving multiple cookies globally
Using Cypress.Cookies.defaults with a preserve array keeps specified cookies across all tests automatically.
Step 2: Compare with other options
Calling preserveOnce in every test is repetitive. Manually setting cookies each time is inefficient. Disabling cookie clearing globally is not recommended.
Final Answer:
Use Cypress.Cookies.defaults({ whitelist: ['token', 'user_id'] }) in support file -> Option D
Quick Check:
Defaults preserve multiple cookies globally [OK]
Hint: Use Cypress.Cookies.defaults to preserve cookies globally [OK]
Common Mistakes:
Calling preserveOnce repeatedly instead of defaults
Manually resetting cookies each test
Trying to disable cookie clearing globally
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.