Bird
0
0

How can you read a JSON file and modify a property before using it in a test?

hard📝 Application Q9 of 15
Cypress - File Operations
How can you read a JSON file and modify a property before using it in a test?
Choose the correct approach.
Acy.readFile('data.json').then(data => data.newProp = 123); cy.log(data.newProp)
Bconst data = cy.readFile('data.json'); data.newProp = 123;
Ccy.readFile('data.json').newProp = 123;
Dcy.readFile('data.json').then(data => { data.newProp = 123; /* use data here */ })
Step-by-Step Solution
Solution:
  1. Step 1: Use .then() to access and modify JSON data

    Modification must happen inside the .then() callback where data is available.
  2. Step 2: Avoid accessing data outside the callback

    Assigning or modifying outside causes errors or undefined behavior.
  3. Final Answer:

    cy.readFile('data.json').then(data => { data.newProp = 123; /* use data here */ }) -> Option D
  4. Quick Check:

    Modify data inside .then() callback only [OK]
Quick Trick: Modify JSON data only inside .then() callback [OK]
Common Mistakes:
  • Trying to assign outside .then()
  • Assuming cy.readFile returns data synchronously
  • Accessing modified data outside callback

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes