How to Assert API Response in Cypress: Simple Guide
In Cypress, you can assert API responses using
cy.request() to make the call and .then() to check the response. Use expect() assertions inside the callback to verify status codes, body content, or headers.Syntax
The basic syntax to assert an API response in Cypress involves using cy.request() to send the HTTP request, followed by .then() to access the response object. Inside the callback, use expect() to check properties like status, body, or headers.
Example parts explained:
cy.request('GET', url): Sends a GET request to the specified URL..then((response) => { ... }): Callback to handle the response.expect(response.status).to.eq(200): Asserts the HTTP status code is 200.expect(response.body).to.have.property('key', 'value'): Checks if response body has a key with a specific value.
javascript
cy.request('GET', '/api/users').then((response) => { expect(response.status).to.eq(200) expect(response.body).to.have.property('users') })
Example
This example demonstrates how to send a GET request to an API endpoint and assert the response status and body content. It checks that the status code is 200 and the response body contains a list of users.
javascript
describe('API Response Assertion', () => { it('should return users with status 200', () => { cy.request('GET', 'https://jsonplaceholder.typicode.com/users').then((response) => { expect(response.status).to.eq(200) expect(response.body).to.be.an('array') expect(response.body[0]).to.have.property('id') expect(response.body[0]).to.have.property('name') }) }) })
Output
Test passes if status is 200 and response body is an array with user objects containing 'id' and 'name'.
Common Pitfalls
Common mistakes when asserting API responses in Cypress include:
- Not waiting for the
cy.request()to complete before asserting, causing flaky tests. - Using incorrect property names in assertions, leading to false failures.
- Not handling asynchronous nature properly by missing
.then()callback. - Assuming response body structure without checking, which can cause errors if API changes.
Always check the actual response structure and use proper chaining.
javascript
/* Wrong way: Missing .then() causes no access to response */ cy.request('GET', '/api/users') // expect(response.status).to.eq(200) // 'response' is undefined here /* Right way: Use .then() to access response */ cy.request('GET', '/api/users').then((response) => { expect(response.status).to.eq(200) })
Quick Reference
| Command | Description | Example |
|---|---|---|
| cy.request(method, url) | Send HTTP request | cy.request('GET', '/api/data') |
| .then(response => {...}) | Access response object | cy.request(...).then(response => {...}) |
| expect(response.status).to.eq(code) | Assert HTTP status code | expect(response.status).to.eq(200) |
| expect(response.body).to.have.property(key, value) | Assert body property | expect(response.body).to.have.property('id', 1) |
| expect(response.body).to.be.an(type) | Assert body type | expect(response.body).to.be.an('array') |
Key Takeaways
Use cy.request() with .then() to access and assert API responses in Cypress.
Always assert the response status code before checking body content.
Check the actual response structure to write accurate assertions.
Avoid accessing response outside .then() callback to prevent errors.
Use clear and specific expect() assertions for reliable tests.