What if you could catch hidden request errors before users even see them?
Why Asserting request properties in Cypress? - Purpose & Use Cases
Imagine testing a web app where you must check if every network request sends the right data. You open the browser, watch the network tab, and try to remember if each request had the correct headers or body. This feels like watching a slow movie and guessing if the story is right.
Manually checking requests is slow and tiring. You can easily miss mistakes because you have to watch many requests and remember details. It's like trying to catch raindrops with your hands--some slip away unnoticed. This causes bugs to sneak into the app and frustrates testers.
Asserting request properties with Cypress lets you automatically check every detail of network requests during tests. You write simple commands that watch requests and confirm they have the right data. This saves time, avoids human error, and makes tests reliable and repeatable.
Open DevTools > Network tab > Watch requests > Manually check headers and bodycy.intercept('POST', '/api/login').as('loginRequest') cy.wait('@loginRequest').its('request.body').should('include', { username: 'user1' })
It enables fast, automatic checks that catch mistakes early, making your app stronger and your testing smarter.
When testing a login form, you can assert that the request sends the correct username and password every time, so users won't face login errors caused by wrong data.
Manual request checks are slow and error-prone.
Asserting request properties automates and speeds up validation.
This leads to more reliable tests and better app quality.
Practice
cy.intercept in Cypress tests?Solution
Step 1: Understand
cy.interceptrolecy.interceptis used to monitor or stub network requests in Cypress tests.Step 2: Identify correct purpose
It helps watch and control HTTP requests, not UI or styling tasks.Final Answer:
To watch and control network requests during test execution -> Option AQuick Check:
cy.intercept = watch/control requests [OK]
- Confusing intercept with UI manipulation
- Thinking intercept generates test data
- Mixing intercept with CSS styling
cy.intercept callback?Solution
Step 1: Identify correct assertion syntax
Cypress uses Chai assertions likeexpect(value).to.equal(expected).Step 2: Match method and case sensitivity
HTTP methods are uppercase strings, so 'POST' must be exact.Final Answer:
expect(req.method).to.equal('POST') -> Option DQuick Check:
expect().to.equal() with exact case [OK]
- Using lowercase 'post' instead of 'POST'
- Using assert syntax instead of expect
- Checking wrong method like 'GET'
cy.intercept('POST', '/api/login', (req) => {
expect(req.body).to.have.property('username', 'admin')
})Solution
Step 1: Analyze the assertion target
The assertion checksreq.bodyfor a property named 'username' with value 'admin'.Step 2: Understand request vs response and headers
It does not check URL, response, or headers, only the request body.Final Answer:
The request body must include a username property with value 'admin' -> Option BQuick Check:
expect(req.body).to.have.property('username', 'admin') [OK]
- Confusing request body with URL query
- Checking response instead of request
- Looking at headers instead of body
cy.intercept('GET', '/api/data', (req) => {
expect(req.headers['Content-Type']).to.equal('application/json')
})Solution
Step 1: Check header key case sensitivity
HTTP headers are case-insensitive, but Cypress normalizes header keys to lowercase in thereq.headersobject. The code uses'Content-Type'which is incorrect; it should be'content-type'.Step 2: Validate other parts
Method GET is valid, assertion syntax is correct, andreqis proper for request interception.Final Answer:
Headers keys are case-insensitive; 'Content-Type' should be 'content-type' -> Option CQuick Check:
Cypress req.headers use lowercase keys [OK]
- Using wrong HTTP method
- Misusing response object instead of request
- Incorrect assertion syntax
/api/submit contains a JSON body with a non-empty email property and a header Authorization starting with 'Bearer '. Which is the best way to write this assertion inside cy.intercept?Solution
Step 1: Check email property assertion
cy.intercept('POST', '/api/submit', (req) => { expect(req.body.email).to.be.a('string').and.not.be.empty expect(req.headers.authorization).to.match(/^Bearer /) })assertsemailis a non-empty string usingto.be.a('string').and.not.be.empty, which is precise.Step 2: Check Authorization header assertion
cy.intercept('POST', '/api/submit', (req) => { expect(req.body.email).to.be.a('string').and.not.be.empty expect(req.headers.authorization).to.match(/^Bearer /) })uses regexto.match(/^Bearer /)to ensure header starts with 'Bearer ', which is exact and case-sensitive.Step 3: Compare other options
cy.intercept('POST', '/api/submit', (req) => { expect(req.body.email).to.exist expect(req.headers.authorization).to.include('Bearer') })uses correct lowercase header key, but .to.exist only checks existence (could be empty or falsy), and .to.include checks substring anywhere, not prefix.cy.intercept('POST', '/api/submit', (req) => { assert.isString(req.body.email) assert.include(req.headers.authorization, 'Bearer') })uses non-standardassertstyle and misses non-empty check; include not prefix.cy.intercept('POST', '/api/submit', (req) => { expect(req.body.email).to.be.empty expect(req.headers.authorization).to.equal('Bearer') })incorrectly expects empty email and exact 'Bearer' match only.Final Answer:
expect(req.body.email).to.be.a('string').and.not.be.empty
expect(req.headers.authorization).to.match(/^Bearer /) -> Option AQuick Check:
Use precise type and regex assertions for properties [OK]
- Ignoring case sensitivity in headers
- Checking only existence, not content
- Using wrong assertion style inconsistently
Ready to prove it?
Pass the test and this pattern moves unknown
