Bird
0
0

You wrote this Cypress code to cache login but tests still run login steps every time:

medium📝 Debug Q14 of 15
Cypress - Authentication and Sessions
You wrote this Cypress code to cache login but tests still run login steps every time:
beforeEach(() => {
  cy.session('user', () => {
    cy.visit('/login')
    cy.get('#username').type('user')
    cy.get('#password').type('pass')
    cy.get('button[type=submit]').click()
  })
})
What is the likely mistake?
AUsing <code>cy.session</code> inside <code>beforeEach</code> instead of <code>before</code>
BNot returning a value or promise inside the <code>cy.session</code> callback
CMissing <code>cy.clearCookies()</code> before <code>cy.session</code>
DNot calling <code>cy.login()</code> instead of <code>cy.session()</code>
Step-by-Step Solution
Solution:
  1. Step 1: Check cy.session() callback requirements

    The callback must return a Cypress chainable or promise to signal completion for caching.
  2. Step 2: Identify why login runs every time

    If the callback does not return, Cypress cannot cache the session, so login runs every test.
  3. Final Answer:

    Not returning a value or promise inside the cy.session callback -> Option B
  4. Quick Check:

    Return chainable in cy.session callback [OK]
Quick Trick: Always return Cypress commands inside cy.session callback [OK]
Common Mistakes:
  • Putting cy.session in beforeEach is allowed and not a mistake
  • Assuming clearing cookies is required before session
  • Confusing cy.login() with cy.session()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes