Bird
0
0

Given the code below, what will be the value of token after the test runs?

medium📝 Predict Output Q13 of 15
Cypress - Authentication and Sessions
Given the code below, what will be the value of token after the test runs?
let token = '';
cy.request('POST', '/api/login', { username: 'user', password: 'pass' })
  .then((response) => {
    token = response.body.token;
  });
AAn empty string because token is declared outside the callback
BThe token string returned by the API in response.body.token
CUndefined because response.body.token does not exist
DThe entire response object
Step-by-Step Solution
Solution:
  1. Step 1: Understand asynchronous assignment

    The token variable is assigned inside the .then() callback after the API response arrives.
  2. Step 2: Confirm token value after request

    response.body.token contains the token string from the API, so token gets that value.
  3. Final Answer:

    The token string returned by the API in response.body.token -> Option B
  4. Quick Check:

    token = response.body.token string [OK]
Quick Trick: Token is set inside .then() from response.body.token [OK]
Common Mistakes:
  • Assuming token stays empty due to async
  • Thinking token is undefined without checking API
  • Confusing token with full response object

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes