https://github.com/elaichenkov/cypress-should
Supercool assertion library for Cypress
https://github.com/elaichenkov/cypress-should
api chai cypress testing
Last synced: 9 months ago
JSON representation
Supercool assertion library for Cypress
- Host: GitHub
- URL: https://github.com/elaichenkov/cypress-should
- Owner: elaichenkov
- License: mit
- Created: 2023-07-28T18:56:20.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-09-30T10:35:36.000Z (about 1 year ago)
- Last Synced: 2024-10-11T04:46:42.045Z (about 1 year ago)
- Topics: api, chai, cypress, testing
- Language: JavaScript
- Homepage:
- Size: 984 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://vshymanskyy.github.io/StandWithUkraine)
# cypress-should
---
[](https://github.com/elaichenkov/cypress-should/actions/workflows/tests.yml)
[](https://www.npmjs.com/package/cypress-should)
[](https://www.npmjs.com/package/cypress-should)
[](https://www.npmjs.com/package/cypress-should)
[](https://github.com/elaichenkov/cypress-should/commits/main)
[](LICENSE)
> Supercool assertion library for Cypress to verify API responses
## Install
```bash
npm install --save-dev cypress-should
```
## Usage
In your `cypress/support/index.js` file, add the following:
```javascript
require('cypress-should');
```
or if you are using TypeScript:
```typescript
import 'cypress-should';
```
Then in your tests, you can use the `should` command with custom assertions to verify the response:
```javascript
cy.request('/api/users')
.should('have.status', 200)
.and('have.statusMessage', 'OK')
.and('have.contentType', 'application/json')
.and('have.body', { name: 'John Doe' });
```
or
```javascript
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' });
});
```
## API
`have.status` - Asserts the response status code
```javascript
cy.request('/api/users').should('have.status', 200)
```
`have.statusMessage` - Asserts the response status message
```javascript
cy.request('/api/users').should('have.statusMessage', 'OK')
```
`have.contentType` - Asserts the response content type
```javascript
cy.request('/api/users').should('have.contentType', 'application/json')
```
`have.body` - Asserts the response body
```javascript
cy.request('/api/users').should('have.body', { name: 'John Doe' })
```
`have.header` - Asserts the response header
```javascript
cy.request('/api/users').should('have.header', 'x-powered-by', 'Express')
```
`have.cookie` - Asserts the response cookie
```javascript
cy.request('/profile').should('have.cookie', 'session_id')
```
`containBody` - Asserts the response body contains the given value
```javascript
cy.request('/api/users').should('containBody', { name: 'John Doe' });
```
`haveText` - Asserts the response body is equal to the given text
```javascript
cy.request('/home').should('haveText', 'Hello World!');
```
`containText` - Asserts the response body contains the given text
```javascript
cy.request('/home').should('containText', 'World!');
```
`be.json` - Asserts the response body is a valid JSON
```javascript
cy.request('/api/users').should('be.json');
```
`be.html` - Asserts the response body is a valid HTML
```javascript
cy.request('/home').should('be.html');
```
`be.text` - Asserts the response body is a valid text
```javascript
cy.request('/home').should('be.text');
```
`be.xml` - Asserts the response body is a valid XML
```javascript
cy.request('/home').should('be.xml');
```
## License
MIT
## Author
Yevhen Laichenkov