We use expect() to check if something in our app works as we want. It helps us catch mistakes early.
expect() for BDD assertions in Cypress
Introduction
Syntax
Cypress
expect(actualValue).to.be.expectedCondition(expectedValue)
actualValue is what you get from your app.
expectedCondition is what you want to check, like equal, contain, or be.visible.
Examples
Cypress
expect(5).to.equal(5)
Cypress
expect('Hello').to.contain('ell')
Cypress
expect(true).to.be.true
Cypress
cy.get('button').then(($button) => { expect($button).to.be.visible })
Sample Program
This test opens a page and checks if the main heading text is exactly 'Kitchen Sink'.
Cypress
describe('My First Test', () => { it('checks if the heading is correct', () => { cy.visit('https://example.cypress.io') cy.get('h1').then(($heading) => { expect($heading.text()).to.equal('Kitchen Sink') }) }) })
Important Notes
Always use expect() inside a test to make sure your app behaves as expected.
Use clear and simple conditions to keep tests easy to understand.
Remember that expect() works well with Cypress commands that return values.
Summary
expect() helps check if your app works right.
Use it to compare actual results with what you want.
It makes tests clear and easy to read.
Practice
1. What is the main purpose of using
expect() in Cypress tests?easy
Solution
Step 1: Understand the role of
expect()expect()is used to compare actual results with expected results in tests.Step 2: Identify the correct purpose
It helps verify if the app behaves as expected by checking values.Final Answer:
To check if the actual value matches the expected value -> Option DQuick Check:
expect() compares values [OK]
Hint: Remember: expect() checks actual vs expected values [OK]
Common Mistakes:
- Thinking expect() runs tests
- Confusing expect() with element selectors
- Using expect() to log messages
2. Which of the following is the correct syntax to assert that a variable
count equals 5 using expect() in Cypress?easy
Solution
Step 1: Recall Cypress BDD assertion syntax
Cypress uses Chai assertions whereto.equal()checks equality.Step 2: Identify the correct syntax
expect(count).to.equal(5);is the correct way to assert equality.Final Answer:
expect(count).to.equal(5); -> Option AQuick Check:
Useto.equal()for equality [OK]
Hint: Use to.equal() for equality checks in Cypress [OK]
Common Mistakes:
- Using toBe() which is Jest syntax
- Using equals() without to
- Using is() which is invalid
3. What will be the result of this Cypress assertion?
const title = 'Welcome'; expect(title).to.have.length(7);
medium
Solution
Step 1: Check the string length
The string 'Welcome' has exactly 7 characters.Step 2: Understand the assertion
to.have.length(7)correctly asserts the string length is 7.Final Answer:
Test passes because 'Welcome' has 7 characters -> Option AQuick Check:
String length 7 matches assertion [OK]
Hint: Count characters to verify length assertions [OK]
Common Mistakes:
- Assuming length counts words, not characters
- Confusing length with array length
- Using wrong assertion method
4. Identify the error in this Cypress assertion:
expect(user.name).to.equals('Alice');medium
Solution
Step 1: Check the assertion method
The correct Chai assertion method isto.equal, notto.equals.Step 2: Confirm syntax correctness
Usingto.equalscauses a runtime error because it is not defined.Final Answer:
Incorrect method name; should beto.equal-> Option BQuick Check:
Use to.equal, not to.equals [OK]
Hint: Remember: method is to.equal, not to.equals [OK]
Common Mistakes:
- Using to.equals instead of to.equal
- Confusing property access syntax
- Assuming expect needs parentheses after property
5. You want to check that an array
items contains the string 'apple' using expect(). Which assertion correctly tests this?hard
Solution
Step 1: Understand the goal
You want to check if 'apple' is inside the arrayitems.Step 2: Choose the correct assertion
to.include('apple')checks if the array contains that element.Final Answer:
expect(items).to.include('apple'); -> Option CQuick Check:
Use to.include() to check array contents [OK]
Hint: Use to.include() to check if array has an item [OK]
Common Mistakes:
- Using to.equal() which checks full equality
- Using length assertions incorrectly
- Combining contain and length wrongly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
