2.6.0 • Published 2 years ago

@nrk/expect-openapi v2.6.0

Weekly downloads
319
License
MIT
Repository
github
Last release
2 years ago

expect-openapi npm

Expect objects to match OpenAPI documents

Setup

yarn add -D @nrk/expect-openapi
# or
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"; // OpenAPI 3 schema

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");
    // Asserts that:
    // - mime-type matches
    // - response code exists (or default)
    // - the schema matches the response body (assumes it is JSON)
    await expect(response).toMatchApiResponse(openapi, "get", "/fantastic");
  });
});

Expect object to match a part of a OpenAPI document

import openapi from "./openapi.json"; // OpenAPI 3 schema

describe("my complex api", () => {
  it("should match SimpleLink", async () => {
    const myLink = { href: "/my/fantastic/link" };
    // Asserts that:
    // - the reference can be resolved
    // - the resolved schema matches expected value
    await expect(myLink).toMatchRef$(
      openapi,
      "#/components/schemas/SimpleLink"
    );
  });
});
2.6.0

2 years ago

2.5.0

3 years ago

2.3.0

4 years ago

2.4.0

4 years ago

2.2.0

4 years ago

2.1.0

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.1.0

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago