https://github.com/elaichenkov/odottaa
🦥 Custom playwright matchers to test the state of the API response
https://github.com/elaichenkov/odottaa
api assertions e2e expect playwright testing typescript
Last synced: over 1 year ago
JSON representation
🦥 Custom playwright matchers to test the state of the API response
- Host: GitHub
- URL: https://github.com/elaichenkov/odottaa
- Owner: elaichenkov
- License: mit
- Created: 2022-03-31T19:33:44.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2023-12-19T17:12:09.000Z (over 2 years ago)
- Last Synced: 2023-12-20T12:10:35.918Z (over 2 years ago)
- Topics: api, assertions, e2e, expect, playwright, testing, typescript
- Language: TypeScript
- Homepage:
- Size: 3.29 MB
- Stars: 32
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://vshymanskyy.github.io/StandWithUkraine)
---
[](https://github.com/elaichenkov/odottaa/actions/workflows/tests.yml)
[](https://www.npmjs.com/package/odottaa)
[](https://www.npmjs.com/package/odottaa)
[](https://www.npmjs.com/package/odottaa)
[](https://github.com/elaichenkov/odottaa/commits/main)
[](LICENSE)
## Table of Contents
- [Table of Contents](#table-of-contents)
- [Installation](#installation)
- [Usage](#usage)
- [TypeScript](#typescript)
- [JavaScript](#javascript)
- [API](#api)
- [toHaveStatusCode](#tohavestatuscode)
- [toHaveStatusText](#tohavestatustext)
- [toBeCreated](#tobecreated)
- [toBeUnauthorized](#tobeunauthorized)
- [toBeForbidden](#tobeforbidden)
- [toBeNotFound](#tobenotfound)
- [toHaveJSON](#tohavejson)
- [toContainJSON](#tocontainjson)
- [toMatchJSON](#tomatchjson)
- [toHaveHeader](#tohaveheader)
- [toHaveHeaders](#tohaveheaders)
- [toHaveContentType](#tohavecontenttype)
- [toContainTextContent](#tocontaintextcontent)
- [toHaveLocation](#tohavelocation)
- [toBeRedirected](#toberedirected)
- [Author](#author)
- [License](#license)
## Installation
This module is distributed via [npm](https://npmjs.com/) which is bundled with [node](https://nodejs.org/) and
should be installed as one of your project's `devDependencies`:
```bash
npm i -D odottaa
```
## Usage
### TypeScript
1. Import `odottaa` module
2. Extend expect with custom API matchers
```typescript
// 1. In your playwright.config.ts
import { expect } from '@playwright/test';
import playwrightApiMatchers from 'odottaa';
// 2. extend expect with custom API matchers
expect.extend(playwrightApiMatchers);
```
### JavaScript
```javascript
// 1. In your playwright.config.js
const { expect } = require('@playwright/test');
const { default: playwrightApiMatchers } = require('odottaa');
// 2. extend expect with custom API matchers
expect.extend(playwrightApiMatchers);
```
## API
### toHaveStatusCode
Use `toHaveStatusCode` matcher to verify that the response's status code is equal to the expected status code
```typescript
const response = await request.get('https://example.com/');
await expect(response).toHaveStatusCode(201);
```
### toHaveStatusText
Use `toHaveStatusText` matcher to verify that the response' status text is equal to the expected status text
```typescript
const response = await request.get('https://example.com/404');
await expect(response).toHaveStatusText('Not Found');
```
### toBeCreated
Use `toBeCreated` matcher to verify that the response's status code is 201
```typescript
const response = await request.post('https://example.com/create');
await expect(response).toBeCreated();
```
### toBeUnauthorized
Use `toBeUnauthorized` matcher to verify that the response's status code is 401
```typescript
const response = await request.post('https://example.com/create');
await expect(response).toBeUnauthorized();
```
### toBeForbidden
Use `toBeForbidden` matcher to verify that the response's status code is 403
```typescript
const response = await request.post('https://example.com/create');
await expect(response).toBeForbidden();
```
### toBeNotFound
Use `toBeNotFound` matcher to verify that the response's status code is 404
```typescript
const response = await request.post('https://example.com/list');
await expect(response).toBeNotFound();
```
### toHaveJSON
Use `toHaveJSON` matcher to verify that the response's body json is equal to the all properties of object instances (also known as "deep" equality)
```typescript
const response = await request.get('https://example.com/data.json');
// e.g. response { name: 'Ben', age: 37 }
await expect(response).toHaveJSON({
name: 'Ben',
age: 37
});
```
### toContainJSON
Use `toContainJSON` matcher to verify that the response's body array contains that an item with a specific structure and values is contained in an array.
```typescript
const response = await request.get('https://example.com/data.json');
// e.g. response [{ name: 'Ben', age: 37 }, { name: 'Anna', age: 26 }]
await expect(response).toContainJSON({
name: 'Ben',
age: 37
});
```
### toMatchJSON
Use `toMatchJSON` matcher to verify that the response's body json matches a subset of the properties of an object. It'll match received objects with properties that are not in the expected object.
```typescript
const response = await request.get('https://example.com/data.json');
// e.g. response [{ name: 'Ben', age: 37 }, { name: 'Anna', age: 26 }]
await expect(response).toMatchJSON({
name: 'John Doe',
});
```
### toHaveHeader
Use `toHaveHeader` matcher to verify that the response's headers contains the expected header and value
```typescript
const response = await request.get('https://example.com');
// Asserts that the response's headers contains the header 'content-length'
await expect(response).toHaveHeader('content-length');
// Asserts that the response's headers contains the header 'content-length' with value '22'
await expect(response).toHaveHeader('content-length', '22');
```
### toHaveHeaders
Use `toHaveHeaders` matcher to verify that the response's headers contains the expected header
```typescript
const response = await request.get('https://example.com');
// Single
await expect(response).toHaveHeaders({ 'content-length': '22' });
// Multiple
await expect(response).toHaveHeaders({ 'content-type': 'text/html', 'content-length': '22' });
```
### toHaveContentType
Use `toHaveContentType` matcher to verify that the response' headers content type is equal to the expected type
```typescript
const response = await request.get('https://example.com/');
await expect(response).toHaveContentType('text/html');
```
### toContainTextContent
Use `toContainTextContent` matcher to verify that the response' body text contains the expected text
```typescript
const response = await request.get('https://example.com/');
await expect(response).toContainTextContent('Hello, World!');
```
### toHaveLocation
Use `toHaveLocation` matcher to verify that the response' headers location is equal to the expected location
```typescript
const response = await request.get('https://example.com/');
await expect(response).toHaveLocation('/home');
```
### toBeRedirected
Use `toBeRedirected` matcher to verify that the response' url is being redirected to the expected url
```typescript
const response = await request.get('https://example.com/user/profile');
await expect(response).toBeRedirected('https://example.com/auth/login');
```
## Author
Yevhen Laichenkov
## License
[MIT](LICENSE)