Skip to content

Commit 06f6c15

Browse files
author
ChaiJs Bot
committed
(data): Auto build _data
1 parent 5c4f7e5 commit 06f6c15

3 files changed

Lines changed: 35 additions & 22 deletions

File tree

_data/plugins/chai_openapi_response_validator.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

_data/releases.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,7 @@
11621162
"content_type": "text/javascript",
11631163
"state": "uploaded",
11641164
"size": 117322,
1165-
"download_count": 7646,
1165+
"download_count": 7648,
11661166
"created_at": "2014-01-30T00:03:47Z",
11671167
"updated_at": "2014-01-30T00:03:53Z",
11681168
"browser_download_url": "https://github.com/chaijs/chai/releases/download/1.9.0/chai.js"

plugins/chai_openapi_response_validator.md

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,21 @@ pluginName: chai-openapi-response-validator
1212
![dependencies](https://img.shields.io/david/RuntimeTools/OpenAPIValidators)
1313
![style](https://img.shields.io/badge/code%20style-airbnb-ff5a5f.svg)
1414
[![codecov](https://codecov.io/gh/RuntimeTools/OpenAPIValidators/branch/master/graph/badge.svg)](https://codecov.io/gh/RuntimeTools/OpenAPIValidators)
15+
[![included](https://badgen.net/npm/types/jest-openapi)](https://github.com/RuntimeTools/OpenAPIValidators/blob/master/packages/chai-openapi-response-validator/index.d.ts)
16+
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/RuntimeTools/OpenAPIValidators/blob/master/CONTRIBUTING.md)
1517

1618
Simple Chai support for asserting that HTTP responses satisfy an OpenAPI spec.
1719

18-
## Problem
20+
## Problem 😕
1921

2022
If your server's behaviour doesn't match your API documentation, then you need to correct your server, your documentation, or both. The sooner you know the better.
2123

22-
## Solution
24+
## Solution 😄
2325

2426
This plugin lets you automatically test whether your server's behaviour and documentation match. It extends the [Chai Assertion Library](https://www.chaijs.com/) to support the [OpenAPI standard](https://swagger.io/docs/specification/about/) for documenting REST APIs. In your JavaScript tests, you can simply assert [`expect(responseObject).to.satisfyApiSpec`](#in-api-tests-validate-the-status-and-body-of-http-responses-against-your-openapi-spec)
2527

2628
Features:
29+
2730
- Validates the status and body of HTTP responses against your OpenAPI spec [(see example)](#in-api-tests-validate-the-status-and-body-of-http-responses-against-your-openapi-spec)
2831
- Validates objects against schemas defined in your OpenAPI spec [(see example)](#in-unit-tests-validate-objects-against-schemas-defined-in-your-OpenAPI-spec)
2932
- Load your OpenAPI spec just once in your tests (load from a filepath or object)
@@ -35,21 +38,24 @@ Features:
3538
- Bundled with a TypeScript Declaration File for [use in TypeScript projects](#using-this-plugin-in-a-typescript-project)
3639
- Use in [Mocha](#usage), [Jest](https://github.com/RuntimeTools/OpenAPIValidators/tree/master/packages/jest-openapi#readme) and other test runners
3740

38-
## Contributing
41+
## Contributing
3942

4043
If you've come here to help contribute - thanks! Take a look at the [contributing](https://github.com/RuntimeTools/OpenAPIValidators/blob/master/CONTRIBUTING.md) docs to get started.
4144

4245
## Installation
46+
4347
This is an addon plugin for the [Chai Assertion Library](http://chaijs.com). Install via [npm](http://npmjs.org).
48+
4449
```bash
45-
$ npm install --save-dev chai-openapi-response-validator
50+
npm install --save-dev chai-openapi-response-validator
4651
```
4752

4853
## Usage
4954

5055
### In API tests, validate the status and body of HTTP responses against your OpenAPI spec:
5156

5257
#### 1. Write a test:
58+
5359
```javascript
5460
// Set up Chai
5561
const chai = require('chai');
@@ -62,9 +68,8 @@ const chaiResponseValidator = require('chai-openapi-response-validator');
6268
chai.use(chaiResponseValidator('path/to/openapi.yml'));
6369

6470
// Write your test (e.g. using Mocha)
65-
describe('GET /example/endpoint', function() {
66-
it('should satisfy OpenAPI spec', async function() {
67-
71+
describe('GET /example/endpoint', () => {
72+
it('should satisfy OpenAPI spec', async () => {
6873
// Get an HTTP response from your server (e.g. using axios)
6974
const res = await axios.get('http://localhost:3000/example/endpoint');
7075

@@ -77,6 +82,7 @@ describe('GET /example/endpoint', function() {
7782
```
7883

7984
#### 2. Write an OpenAPI Spec (and save to `path/to/openapi.yml`):
85+
8086
```yaml
8187
openapi: 3.0.0
8288
info:
@@ -131,6 +137,7 @@ paths:
131137
```
132138

133139
###### Output from test failure:
140+
134141
```javascript
135142
AssertionError: expected res to satisfy API spec
136143
@@ -162,6 +169,7 @@ The '200' response defined for endpoint 'GET /example/endpoint' in API spec: {
162169
### In unit tests, validate objects against schemas defined in your OpenAPI spec:
163170

164171
#### 1. Write a test:
172+
165173
```javascript
166174
// Set up Chai
167175
const chai = require('chai');
@@ -174,8 +182,8 @@ const chaiResponseValidator = require('chai-openapi-response-validator');
174182
chai.use(chaiResponseValidator('path/to/openapi.yml'));
175183
176184
// Write your test (e.g. using Mocha)
177-
describe('myModule.getObject()', function() {
178-
it('should satisfy OpenAPI spec', async function() {
185+
describe('myModule.getObject()', () => {
186+
it('should satisfy OpenAPI spec', async () => {
179187
// Run the function you want to test
180188
const myModule = require('path/to/your/module.js');
181189
const output = myModule.getObject();
@@ -187,6 +195,7 @@ describe('myModule.getObject()', function() {
187195
```
188196

189197
#### 2. Write an OpenAPI Spec (and save to `path/to/openapi.yml`):
198+
190199
```yaml
191200
openapi: 3.0.0
192201
info:
@@ -227,7 +236,6 @@ components:
227236
};
228237
```
229238

230-
231239
##### The assertion fails if the object does not satisfy the schema `ExampleSchemaObject`:
232240

233241
```javascript
@@ -272,7 +280,9 @@ The 'ExampleSchemaObject' schema in API spec: {
272280
### Loading your OpenAPI spec (3 different ways):
273281

274282
#### 1. From an absolute filepath ([see above](#usage))
283+
275284
#### 2. From an object:
285+
276286
```javascript
277287
// Set up Chai
278288
const chai = require('chai');
@@ -292,7 +302,7 @@ const openApiSpec = {
292302
'/example/endpoint': {
293303
get: {
294304
responses: {
295-
'200': {
305+
200: {
296306
description: 'Response body should be a string',
297307
content: {
298308
'application/json': {
@@ -312,9 +322,8 @@ const openApiSpec = {
312322
chai.use(chaiResponseValidator(openApiSpec));
313323
314324
// Write your test (e.g. using Mocha)
315-
describe('GET /example/endpoint', function() {
316-
it('should satisfy OpenAPI spec', async function() {
317-
325+
describe('GET /example/endpoint', () => {
326+
it('should satisfy OpenAPI spec', async () => {
318327
// Get an HTTP response from your server (e.g. using axios)
319328
const res = await axios.get('http://localhost:3000/example/endpoint');
320329
@@ -327,6 +336,7 @@ describe('GET /example/endpoint', function() {
327336
```
328337

329338
#### 3. From a web endpoint:
339+
330340
```javascript
331341
// Set up Chai
332342
const chai = require('chai');
@@ -336,18 +346,16 @@ const expect = chai.expect;
336346
const chaiResponseValidator = require('chai-openapi-response-validator');
337347
338348
// Write your test (e.g. using Mocha)
339-
describe('GET /example/endpoint', function() {
340-
349+
describe('GET /example/endpoint', () => {
341350
// Load your OpenAPI spec from a web endpoint
342-
before(async function() {
351+
before(async () => {
343352
const axios = require('axios');
344353
const response = await axios.get('url/to/openapi/spec');
345354
const openApiSpec = response.data; // e.g. { openapi: '3.0.0', <etc> };
346355
chai.use(chaiResponseValidator(openApiSpec));
347356
});
348357
349-
it('should satisfy OpenAPI spec', async function() {
350-
358+
it('should satisfy OpenAPI spec', async () => {
351359
// Get an HTTP response from your server (e.g. using axios)
352360
const res = await axios.get('http://localhost:3000/example/endpoint');
353361
@@ -362,18 +370,23 @@ describe('GET /example/endpoint', function() {
362370
### Using this plugin in a TypeScript project
363371

364372
#### Installation
365-
You don't need to `npm install --save-dev @types/chai-openapi-response-validator` because we bundle our TypeScript Definition file into this package (see `index.d.ts`).
373+
374+
You don't need to `npm install --save-dev @types/chai-openapi-response-validator` because we [bundle our TypeScript Definition file into this package](https://github.com/RuntimeTools/OpenAPIValidators/blob/master/packages/chai-openapi-response-validator/index.d.ts).
366375

367376
#### Importing
377+
368378
1. Make sure your `tsconfig.json` includes:
379+
369380
```javascript
370381
{
371382
"compilerOptions": {
372383
esModuleInterop: true,
373384
}
374385
}
375386
```
387+
376388
2. Import like this:
389+
377390
```javascript
378391
import chai from 'chai';
379392
import chaiResponseValidator from 'chai-openapi-response-validator';

0 commit comments

Comments
 (0)