Skip to content

Commit

Permalink
test(int): split type and behavior tests
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph-fricke committed Nov 18, 2023
1 parent 8271f60 commit 85e5c99
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 79 deletions.
File renamed without changes.
25 changes: 25 additions & 0 deletions test/no-content.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { type StrictResponse } from "msw";
import { createOpenApiHttp } from "openapi-msw";
import { describe, expectTypeOf, test } from "vitest";
import type { paths } from "./fixtures/no-content.api.js";

describe("Given an OpenAPI schema endpoint with no-content", () => {
const http = createOpenApiHttp<paths>({ baseUrl: "*" });

test("When an endpoint is mocked, Then responses with content cannot be returned", async () => {
type Endpoint = typeof http.delete<"/resource">;
const resolver = expectTypeOf<Endpoint>().parameter(1);
const response = resolver.returns.extract<Response>();

response.not.toEqualTypeOf<StrictResponse<{ id: number }>>();
});

test("When an endpoint is mocked, Then responses must be strict responses", async () => {
type Endpoint = typeof http.delete<"/resource">;
const resolver = expectTypeOf<Endpoint>().parameter(1);
const response = resolver.returns.extract<Response>();

response.not.toEqualTypeOf<Response>();
response.toEqualTypeOf<StrictResponse<null>>();
});
});
19 changes: 1 addition & 18 deletions test/no-content.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpResponse, type StrictResponse } from "msw";
import { createOpenApiHttp } from "openapi-msw";
import { describe, expect, expectTypeOf, test } from "vitest";
import { describe, expect, test } from "vitest";
import type { paths } from "./fixtures/no-content.api.js";

describe("Given an OpenAPI schema endpoint with no-content", () => {
Expand Down Expand Up @@ -33,21 +33,4 @@ describe("Given an OpenAPI schema endpoint with no-content", () => {
expect(result?.response?.body).toBeNull();
expect(result?.response?.status).toBe(201);
});

test("When an endpoint is mocked, Then responses with content cannot be returned", async () => {
type Endpoint = typeof http.delete<"/resource">;
const resolver = expectTypeOf<Endpoint>().parameter(1);
const response = resolver.returns.extract<Response>();

response.not.toEqualTypeOf<StrictResponse<{ id: number }>>();
});

test("When an endpoint is mocked, Then responses must be strict responses", async () => {
type Endpoint = typeof http.delete<"/resource">;
const resolver = expectTypeOf<Endpoint>().parameter(1);
const response = resolver.returns.extract<Response>();

response.not.toEqualTypeOf<Response>();
response.toEqualTypeOf<StrictResponse<null>>();
});
});
23 changes: 23 additions & 0 deletions test/path-fragments.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { createOpenApiHttp } from "openapi-msw";
import { describe, expectTypeOf, test } from "vitest";
import type { paths } from "./fixtures/path-fragments.api.js";

describe("Given an OpenAPI schema endpoint that contains path fragments", () => {
const http = createOpenApiHttp<paths>({ baseUrl: "*" });

test("When an endpoint is mocked, Then the path fragments are strict-typed", () => {
type Endpoint = typeof http.get<"/resource/{id}/{name}">;
const resolver = expectTypeOf<Endpoint>().parameter(1);
const params = resolver.parameter(0).toHaveProperty("params");

params.toEqualTypeOf<{ id: string; name: string }>();
});

test("When a endpoint contains no path fragments, Then no params are provided", () => {
type Endpoint = typeof http.get<"/resource">;
const resolver = expectTypeOf<Endpoint>().parameter(1);
const params = resolver.parameter(0).toHaveProperty("params");

params.toEqualTypeOf<never>();
});
});
18 changes: 1 addition & 17 deletions test/path-fragments.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpResponse } from "msw";
import { createOpenApiHttp } from "openapi-msw";
import { describe, expect, expectTypeOf, test } from "vitest";
import { describe, expect, test } from "vitest";
import type { paths } from "./fixtures/path-fragments.api.js";

describe("Given an OpenAPI schema endpoint that contains path fragments", () => {
Expand All @@ -21,22 +21,6 @@ describe("Given an OpenAPI schema endpoint that contains path fragments", () =>
expect(responseBody?.name).toBe("test-name");
});

test("When an endpoint is mocked, Then the path fragments are strict-typed", () => {
type Endpoint = typeof http.get<"/resource/{id}/{name}">;
const resolver = expectTypeOf<Endpoint>().parameter(1);
const params = resolver.parameter(0).toHaveProperty("params");

params.toEqualTypeOf<{ id: string; name: string }>();
});

test("When a endpoint contains no path fragments, Then no params are provided", () => {
type Endpoint = typeof http.get<"/resource">;
const resolver = expectTypeOf<Endpoint>().parameter(1);
const params = resolver.parameter(0).toHaveProperty("params");

params.toEqualTypeOf<never>();
});

// FIXME: See https://github.com/christoph-fricke/openapi-msw/issues/22
test.skip("When a path fragments is defined as non-string, Then the fragment is still typed as a string", async () => {
const request = new Request(
Expand Down
34 changes: 34 additions & 0 deletions test/request-body.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { type StrictRequest } from "msw";
import { createOpenApiHttp } from "openapi-msw";
import { describe, expectTypeOf, test } from "vitest";
import type { paths } from "./fixtures/request-body.api.js";

describe("Given an OpenAPI schema endpoint with request content", () => {
const http = createOpenApiHttp<paths>({ baseUrl: "*" });

test("When a request is expected to contain content, Then the content is strict-typed", () => {
type Endpoint = typeof http.post<"/resource">;
const resolver = expectTypeOf<Endpoint>().parameter(1);
const request = resolver.parameter(0).toHaveProperty("request");

request.toEqualTypeOf<StrictRequest<{ name: string; value: number }>>();
});

test("When a request content is optional, Then the content is strict-typed with optional", () => {
type Endpoint = typeof http.patch<"/resource">;
const resolver = expectTypeOf<Endpoint>().parameter(1);
const request = resolver.parameter(0).toHaveProperty("request");

request.toEqualTypeOf<
StrictRequest<{ name: string; value: number } | undefined>
>();
});

test("When a request is not expected to contain content, Then the content is undefined", () => {
type Endpoint = typeof http.get<"/resource">;
const resolver = expectTypeOf<Endpoint>().parameter(1);
const request = resolver.parameter(0).toHaveProperty("request");

request.toEqualTypeOf<StrictRequest<undefined>>();
});
});
30 changes: 2 additions & 28 deletions test/request-body.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpResponse, type StrictRequest } from "msw";
import { HttpResponse } from "msw";
import { createOpenApiHttp } from "openapi-msw";
import { describe, expect, expectTypeOf, test } from "vitest";
import { describe, expect, test } from "vitest";
import type { paths } from "./fixtures/request-body.api.js";

describe("Given an OpenAPI schema endpoint with request content", () => {
Expand Down Expand Up @@ -29,30 +29,4 @@ describe("Given an OpenAPI schema endpoint with request content", () => {
value: 16,
});
});

test("When a request is expected to contain content, Then the content is strict-typed", () => {
type Endpoint = typeof http.post<"/resource">;
const resolver = expectTypeOf<Endpoint>().parameter(1);
const request = resolver.parameter(0).toHaveProperty("request");

request.toEqualTypeOf<StrictRequest<{ name: string; value: number }>>();
});

test("When a request content is optional, Then the content is strict-typed with optional", () => {
type Endpoint = typeof http.patch<"/resource">;
const resolver = expectTypeOf<Endpoint>().parameter(1);
const request = resolver.parameter(0).toHaveProperty("request");

request.toEqualTypeOf<
StrictRequest<{ name: string; value: number } | undefined>
>();
});

test("When a request is not expected to contain content, Then the content is undefined", () => {
type Endpoint = typeof http.get<"/resource">;
const resolver = expectTypeOf<Endpoint>().parameter(1);
const request = resolver.parameter(0).toHaveProperty("request");

request.toEqualTypeOf<StrictRequest<undefined>>();
});
});
19 changes: 19 additions & 0 deletions test/response-content.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { type StrictResponse } from "msw";
import { createOpenApiHttp } from "openapi-msw";
import { describe, expectTypeOf, test } from "vitest";
import type { paths } from "./fixtures/response-content.api.js";

describe("Given an OpenAPI schema endpoint with response content", () => {
const http = createOpenApiHttp<paths>({ baseUrl: "*" });

test("When a JSON endpoint is mocked, Then responses must be a strict content response", async () => {
type Endpoint = typeof http.get<"/resource">;
const resolver = expectTypeOf<Endpoint>().parameter(1);
const response = resolver.returns.extract<Response>();

response.not.toEqualTypeOf<Response>();
response.toEqualTypeOf<
StrictResponse<{ id: string; name: string; value: number }>
>();
});
});
15 changes: 2 additions & 13 deletions test/response-content.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpResponse, type StrictResponse } from "msw";
import { HttpResponse } from "msw";
import { createOpenApiHttp } from "openapi-msw";
import { describe, expect, expectTypeOf, test } from "vitest";
import { describe, expect, test } from "vitest";
import type { paths } from "./fixtures/response-content.api.js";

describe("Given an OpenAPI schema endpoint with response content", () => {
Expand Down Expand Up @@ -42,15 +42,4 @@ describe("Given an OpenAPI schema endpoint with response content", () => {
expect(responseBody).toBe("Hello World");
expect(result?.response?.status).toBe(200);
});

test("When a JSON endpoint is mocked, Then responses must be strict content response", async () => {
type Endpoint = typeof http.get<"/resource">;
const resolver = expectTypeOf<Endpoint>().parameter(1);
const response = resolver.returns.extract<Response>();

response.not.toEqualTypeOf<Response>();
response.toEqualTypeOf<
StrictResponse<{ id: string; name: string; value: number }>
>();
});
});
3 changes: 0 additions & 3 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ export default defineProject({
test: {
name: suite ?? name,
dir: getTestDir(suite),
typecheck: {
include: ["**/*.test.ts"],
},
alias: {
[name]: exports["."].import,
},
Expand Down

0 comments on commit 85e5c99

Please sign in to comment.