Bird
0
0

Identify the error in the following Cypress code snippet that tries to read a file and assert its content:

medium📝 Debug Q14 of 15
Cypress - File Operations
Identify the error in the following Cypress code snippet that tries to read a file and assert its content:
cy.readFile('config.json').then((data) => {
  expect(data).to.equal('config');
});
AIncorrect file path syntax
BMissing catch() to handle errors
CUsing to.equal() instead of to.deep.equal() for object comparison
DreadFile cannot be used with JSON files
Step-by-Step Solution
Solution:
  1. Step 1: Understand data type returned by readFile

    Reading a JSON file returns an object, not a string.
  2. Step 2: Check assertion method

    to.equal() checks strict equality, which fails for objects; to.deep.equal() is needed for object content comparison.
  3. Final Answer:

    Using to.equal() instead of to.deep.equal() for object comparison -> Option C
  4. Quick Check:

    Use deep.equal for object assertions [OK]
Quick Trick: Use deep.equal for objects, equal for primitives [OK]
Common Mistakes:
  • Using to.equal() for objects
  • Ignoring error handling
  • Assuming readFile can't read JSON

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes