Skip to content

yutak23/vitest-openapi

Repository files navigation

vitest-openapi

npm test style included

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';

// 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.

Requirement

If you use TypeScript, need vitest >= 0.31.0 (see the site Extending Matchers).

License

MIT licensed