Bird
0
0

Why does this code fail?

medium📝 Debug Q7 of 15
Cypress - File Operations
Why does this code fail?
cy.readFile('config.json').then((data) => {
data.setting = true
})
cy.log(data.setting)
Acy.log cannot log boolean values
BMissing await keyword before cy.readFile
Cconfig.json cannot be modified in tests
Ddata is not defined outside the .then() callback
Step-by-Step Solution
Solution:
  1. Step 1: Recognize scope of variables in promises

    data exists only inside the .then() callback function.
  2. Step 2: Understand why cy.log fails

    Outside .then(), data is undefined, causing error.
  3. Final Answer:

    data is not defined outside the .then() callback -> Option D
  4. Quick Check:

    Variables inside .then() are scoped locally [OK]
Quick Trick: Use variables only inside .then() or assign externally [OK]
Common Mistakes:
  • Trying to access promise data outside callback
  • Assuming synchronous execution
  • Misunderstanding variable scope

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes