Use Vitest to assert that HTTP responses satisfy an OpenAPI spec.
This library makes jest-openapi functionality available to Vitest.
- 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
, andchai-http
- Use in Vitest
$ npm install vitest-openapi
$ yarn add vitest-openapi
import { describe, expect, it } from 'vitest';
import vitestOpenAPI from 'vitest-openapi';
import axios from 'axios';
// Load an OpenAPI file (YAML or JSON) into this plugin
vitestOpenAPI('path/to/openapi.yml');
// Write your test
describe('GET /example/endpoint', () => {
it('should satisfy OpenAPI spec', async () => {
// Get an HTTP response from your server (e.g. using axios)
const res = await axios.get('http://localhost:3000/example/endpoint');
expect(res.status).toEqual(200);
// Assert that the HTTP response satisfies the OpenAPI spec
expect(res).toSatisfyApiSpec();
});
});
※See also the Usage section of jest-openapi for more information.
If you use TypeScript, need vitest >= 0.31.0
(see the site Extending Matchers).