TypeScript typed wrapper over supertest for testing APIs using OpenAPI spec definitions.
Check the autogenerated documentation here.
Create a type-safe supertest request sender from your OpenAPI spec.
import { createRequestSender } from '@map-colonies/openapi-supertest';
import type { paths, operations } from './generated-openapi-types';
import supertest from 'supertest';
import express from 'express';
const app = express();
const agent = supertest.agent(app);
const requestSender = createRequestSender<paths, operations>(
['getPetById', 'addPet'], // list of operations
agent,
{ baseUrl: '/api/v1' }
);
// Type-safe requests
const response = await requestSender.getPetById({ pathParams: { petId: 1 } });
// response.status is strongly typed
// response.body is strongly typed
Provides a type-safe wrapper over testing frameworks expect:
import { expectResponseStatusFactory } from '@map-colonies/openapi-supertest';
import { expect } from 'vitest'; // or jest
const expectResponseStatus = expectResponseStatusFactory(expect);
// will provide TS error if 200 is not a valid status code for this response
expectResponseStatus(response, 200);