Supercool assertion library for Cypress to verify API responses
npm install --save-dev cypress-should
In your cypress/support/index.js
file, add the following:
require('cypress-should');
or if you are using TypeScript:
import 'cypress-should';
Then in your tests, you can use the should
command with custom assertions to verify the response:
cy.request('/api/users')
.should('have.status', 200)
.and('have.statusMessage', 'OK')
.and('have.contentType', 'application/json')
.and('have.body', { name: 'John Doe' });
or
cy.request('/api/users').should((response) => {
expect(response).to.have.status(200);
expect(response).to.have.statusMessage('OK');
expect(response).to.haveContentType('application/json');
expect(response).to.have.body({ name: 'John Doe' });
});
have.status
- Asserts the response status code
cy.request('/api/users').should('have.status', 200)
have.statusMessage
- Asserts the response status message
cy.request('/api/users').should('have.statusMessage', 'OK')
have.contentType
- Asserts the response content type
cy.request('/api/users').should('have.contentType', 'application/json')
have.body
- Asserts the response body
cy.request('/api/users').should('have.body', { name: 'John Doe' })
have.header
- Asserts the response header
cy.request('/api/users').should('have.header', 'x-powered-by', 'Express')
have.cookie
- Asserts the response cookie
cy.request('/profile').should('have.cookie', 'session_id')
containBody
- Asserts the response body contains the given value
cy.request('/api/users').should('containBody', { name: 'John Doe' });
haveText
- Asserts the response body is equal to the given text
cy.request('/home').should('haveText', 'Hello World!');
containText
- Asserts the response body contains the given text
cy.request('/home').should('containText', 'World!');
be.json
- Asserts the response body is a valid JSON
cy.request('/api/users').should('be.json');
be.html
- Asserts the response body is a valid HTML
cy.request('/home').should('be.html');
be.text
- Asserts the response body is a valid text
cy.request('/home').should('be.text');
be.xml
- Asserts the response body is a valid XML
cy.request('/home').should('be.xml');
MIT
Yevhen Laichenkov elaichenkov@gmail.com