expect-openapi
Expect objects to match OpenAPI documents
Setup
yarn add -D @nrk/expect-openapi
npm add --dev @nrk/expect-openapi
jest.setup.ts
:
import { toMatchApiResponse, toMatchRef$ } from "@nrk/expect-openapi";
import expect from "expect";
expect.extend({ toMatchApiResponse, toMatchRef$ });
update the jest config:
setupFiles: ["<rootDir>/jest.setup.ts"],
Expect response to match OpenAPI path
import superagent from "superagent";
import openapi from "./openapi.json";
describe("my api", () => {
it("should have valid /fantastic response, according to spec", async () => {
const url = `http://paspi.nrk.no/some/fantastic/path`;
const response = await superagent.get(url).accept("application/json");
await expect(response).toMatchApiResponse(openapi, "get", "/fantastic");
});
});
Expect object to match a part of a OpenAPI document
import openapi from "./openapi.json";
describe("my complex api", () => {
it("should match SimpleLink", async () => {
const myLink = { href: "/my/fantastic/link" };
await expect(myLink).toMatchRef$(
openapi,
"#/components/schemas/SimpleLink"
);
});
});