Bird
0
0

Examine this Cypress API-first setup snippet:

medium📝 Predict Output Q4 of 15
Cypress - Test Organization and Patterns
Examine this Cypress API-first setup snippet:
cy.request('POST', '/api/auth', {user: 'test', pass: '1234'}).then((res) => {
  expect(res.status).to.eq(200)
  cy.setCookie('session', res.body.sessionId)
})
cy.visit('/profile')

What is the likely issue with this code?
ACookies cannot be set inside a <code>then</code> callback
BThe <code>cy.request</code> should use GET instead of POST for login
CThe <code>cy.visit</code> runs before the cookie is set, causing authentication failure
DThe <code>expect</code> assertion should be after <code>cy.visit</code>
Step-by-Step Solution
Solution:
  1. Step 1: Analyze command order

    Cypress commands are asynchronous but chained. Here, cy.visit is called immediately after cy.request without waiting for the cookie to be set.
  2. Step 2: Understand impact

    Since the cookie is set inside the then callback, cy.visit runs before the cookie exists, likely causing authentication to fail.
  3. Final Answer:

    The cy.visit runs before the cookie is set, causing authentication failure -> Option C
  4. Quick Check:

    Ensure commands dependent on async results are chained properly [OK]
Quick Trick: Chain dependent commands to ensure correct execution order [OK]
Common Mistakes:
  • Calling cy.visit outside the then callback
  • Using wrong HTTP method for login
  • Misplacing assertions after navigation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes