vitest-openapi
![included](https://badgen.net/npm/types/jest-openapi)
Use Vitest to assert that HTTP responses satisfy an OpenAPI spec.
This library makes jest-openapi functionality available to Vitest.
Features
- Validates the status and body of HTTP responses against your OpenAPI spec (see usage)
- Validates objects against schemas defined in your OpenAPI spec (see usage)
- Load your OpenAPI spec just once in your tests (load from a filepath or object)
- Supports OpenAPI 2 and 3
- Supports OpenAPI specs in YAML and JSON formats
- Supports
$ref
in response definitions (i.e. $ref: '#/definitions/ComponentType/ComponentName'
) - Informs you if your OpenAPI spec is invalid
- Supports responses from
axios
, request-promise
, supertest
, superagent
, and chai-http
- Use in Vitest
Installation
npm
$ npm install vitest-openapi
yarn
$ yarn add vitest-openapi
Usage
import { describe, expect, it } from 'vitest';
import vitestOpenAPI from 'vitest-openapi';
import axios from 'axios';
vitestOpenAPI('path/to/openapi.yml');
describe('GET /example/endpoint', () => {
it('should satisfy OpenAPI spec', async () => {
const res = await axios.get('http://localhost:3000/example/endpoint');
expect(res.status).toEqual(200);
expect(res).toSatisfyApiSpec();
});
});
※See also the Usage section of jest-openapi for more information.
Requirement
If you use TypeScript, need vitest >= 0.31.0
(see the site Extending Matchers).
License
MIT licensed