Bird
0
0

Consider the code:

medium📝 Predict Output Q5 of 15
Cypress - Assertions
Consider the code:
const list = ['apple', 'banana', 'cherry'];
expect(list).to.have.length(4);

What will happen when this assertion runs?
ATest passes because list length is 4
BTest passes because length is ignored
CSyntax error due to wrong method
DTest fails because list length is 3
Step-by-Step Solution
Solution:
  1. Step 1: Check the actual length of the list

    The list has 3 items, so its length is 3.
  2. Step 2: Compare with expected length in assertion

    The assertion expects length 4, which does not match actual length.
  3. Final Answer:

    Test fails because list length is 3 -> Option D
  4. Quick Check:

    Length mismatch = test fails [OK]
Quick Trick: Check actual length before asserting with to.have.length() [OK]
Common Mistakes:
  • Assuming length is 4 without counting
  • Confusing syntax with error
  • Ignoring assertion failure on mismatch

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes