But when you read the file, you get a string instead of an object. What is the problem?
medium
A. The JSON data is passed as a string, not as an object
B. The file path is incorrect
C. cy.writeFile cannot write JSON data
D. The file extension must be .txt
Solution
Step 1: Analyze the data argument
The data is passed as a JSON string, not as a JavaScript object.
Step 2: Understand how cy.writeFile handles data
If you pass a string, it writes it as a string. To write JSON, pass an object directly.
Final Answer:
The JSON data is passed as a string, not as an object -> Option A
Quick Check:
Pass object, not string, to write JSON [OK]
Hint: Pass object, not JSON string, to write JSON files [OK]
Common Mistakes:
Passing JSON as string instead of object
Blaming file path without checking data
Thinking file extension affects JSON writing
5. You want to append a new line 'Test passed' to an existing file 'results.txt' using cy.writeFile(). Which code snippet correctly appends the text without overwriting the file?
hard
A. cy.writeFile('results.txt', 'Test passed', { flag: 'a+' })
B. cy.writeFile('results.txt', 'Test passed')
C. cy.writeFile('results.txt', 'Test passed', { overwrite: false })
D. cy.writeFile('results.txt', 'Test passed', { append: true })
Solution
Step 1: Understand file writing flags
To append text, the file must be opened with append flag, which is 'a+' in Node.js.
Step 2: Check Cypress options for cy.writeFile
Cypress supports passing { flag: 'a+' } to append instead of overwrite.
Final Answer:
cy.writeFile('results.txt', 'Test passed', { flag: 'a+' }) -> Option A
Quick Check:
Use flag 'a+' to append text [OK]
Hint: Use { flag: 'a+' } option to append text [OK]
Common Mistakes:
Omitting flag option causes overwrite
Using non-existent options like append: true
Assuming overwrite: false appends text
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.