From 584dcfc8d454e0f51dacffbcce6012d492848e55 Mon Sep 17 00:00:00 2001 From: Drew Powers Date: Sun, 28 Jan 2024 14:28:44 -0700 Subject: [PATCH] Fix tests --- packages/openapi-fetch/package.json | 2 +- packages/openapi-fetch/src/index.js | 3 +-- .../openapi-fetch/test/fixtures/v7-beta.d.ts | 4 ++++ packages/openapi-fetch/test/index.test.ts | 18 ++++++++++------ packages/openapi-fetch/test/v7-beta.test.ts | 21 +++++++++++++------ 5 files changed, 33 insertions(+), 15 deletions(-) diff --git a/packages/openapi-fetch/package.json b/packages/openapi-fetch/package.json index 622b22b8a..70a870584 100644 --- a/packages/openapi-fetch/package.json +++ b/packages/openapi-fetch/package.json @@ -55,7 +55,7 @@ "lint": "pnpm run \"/^lint:/\"", "lint:js": "eslint \"{src,test}/**/*.{js,ts}\"", "lint:prettier": "prettier --check \"{src,test}/**/*\"", - "generate-types": "cd ../openapi-typescript && pnpm run build && cd ../openapi-fetch ../openapi-typescript/bin/cli.js ./test/fixtures/api.yaml -o ./test/fixtures/v7-beta.test.ts && npx openapi-typescript ./test/fixtures/api.yaml -o ./test/fixtures/api.d.ts", + "generate-types": "cd ../openapi-typescript && pnpm run build && cd ../openapi-fetch && ../openapi-typescript/bin/cli.js ./test/fixtures/api.yaml -o ./test/fixtures/v7-beta.d.ts && npx openapi-typescript ./test/fixtures/api.yaml -o ./test/fixtures/api.d.ts", "pretest": "pnpm run generate-types", "test": "pnpm run \"/^test:/\"", "test:js": "vitest run", diff --git a/packages/openapi-fetch/src/index.js b/packages/openapi-fetch/src/index.js index f526acbcb..b67b2fa75 100644 --- a/packages/openapi-fetch/src/index.js +++ b/packages/openapi-fetch/src/index.js @@ -56,10 +56,9 @@ export default function createClient(clientOptions) { requestInit, ); // remove `Content-Type` if serialized body is FormData; browser will correctly set Content-Type & boundary expression - if (request.body instanceof FormData) { + if (requestInit.body instanceof FormData) { request.headers.delete("Content-Type"); } - // middleware (request) const mergedOptions = { baseUrl, diff --git a/packages/openapi-fetch/test/fixtures/v7-beta.d.ts b/packages/openapi-fetch/test/fixtures/v7-beta.d.ts index ababc0ff2..eb10a870a 100644 --- a/packages/openapi-fetch/test/fixtures/v7-beta.d.ts +++ b/packages/openapi-fetch/test/fixtures/v7-beta.d.ts @@ -645,6 +645,10 @@ export interface components { email: string; age?: number; avatar?: string; + /** Format: date */ + created_at: number; + /** Format: date */ + updated_at: number; }; }; responses: { diff --git a/packages/openapi-fetch/test/index.test.ts b/packages/openapi-fetch/test/index.test.ts index 02c26b0b9..6cef30d2c 100644 --- a/packages/openapi-fetch/test/index.test.ts +++ b/packages/openapi-fetch/test/index.test.ts @@ -522,6 +522,8 @@ describe("client", () => { }); it("can modify response", async () => { + const toUnix = (date: string) => new Date(date).getTime(); + const rawBody = { email: "user123@gmail.com", created_at: "2024-01-01T00:00:00Z", @@ -535,10 +537,11 @@ describe("client", () => { const client = createClient(); client.use({ + // convert date string to unix time async onResponse(res) { const body = await res.json(); - body.created_at = new Date(body.created_at).getTime(); - body.updated_at = new Date(body.updated_at).getTime(); + body.created_at = toUnix(body.created_at); + body.updated_at = toUnix(body.updated_at); const headers = new Headers(res.headers); headers.set("middleware", "value"); return new Response(JSON.stringify(body), { @@ -552,8 +555,8 @@ describe("client", () => { const { data, response } = await client.GET("/self"); // assert body was modified - expect(data?.created_at).toBe(new Date(rawBody.created_at).getTime()); - expect(data?.updated_at).toBe(new Date(rawBody.updated_at).getTime()); + expect(data?.created_at).toBe(toUnix(rawBody.created_at)); + expect(data?.updated_at).toBe(toUnix(rawBody.updated_at)); // assert rest of body was preserved expect(data?.email).toBe(rawBody.email); // assert status changed @@ -694,6 +697,8 @@ describe("client", () => { }); it("can be ejected", async () => { + mockFetchOnce({ status: 200, body: "{}" }); + let called = false; const errorMiddleware = { onRequest() { @@ -754,10 +759,11 @@ describe("client", () => { // expect post_id to be encoded properly const req = fetchMocker.mock.calls[0][0]; - expect(req.body).toBeInstanceOf(FormData); + // note: this is FormData, but Node.js doesn’t handle new Request() properly with formData bodies. So this is only in tests. + expect(req.body).toBeInstanceOf(Buffer); // TODO: `vitest-fetch-mock` does not add the boundary to the Content-Type header like browsers do, so we expect the header to be null instead - expect((req.headers as Headers).get("Content-Type")).toBeNull(); + expect(req.headers.get("Content-Type")).toBeNull(); }); }); diff --git a/packages/openapi-fetch/test/v7-beta.test.ts b/packages/openapi-fetch/test/v7-beta.test.ts index f678b6038..e57fdfbae 100644 --- a/packages/openapi-fetch/test/v7-beta.test.ts +++ b/packages/openapi-fetch/test/v7-beta.test.ts @@ -1,7 +1,10 @@ import { afterEach, beforeAll, describe, expect, it, vi } from "vitest"; // @ts-expect-error import createFetchMock from "vitest-fetch-mock"; -import createClient, { type Middleware } from "../src/index.js"; +import createClient, { + type MiddlewareRequest, + type Middleware, +} from "../src/index.js"; import type { paths } from "./fixtures/v7-beta.js"; // Note @@ -528,6 +531,8 @@ describe("client", () => { }); it("can modify response", async () => { + const toUnix = (date: string) => new Date(date).getTime(); + const rawBody = { email: "user123@gmail.com", created_at: "2024-01-01T00:00:00Z", @@ -541,10 +546,11 @@ describe("client", () => { const client = createClient(); client.use({ + // convert date string to unix time async onResponse(res) { const body = await res.json(); - body.created_at = new Date(body.created_at).getTime(); - body.updated_at = new Date(body.updated_at).getTime(); + body.created_at = toUnix(body.created_at); + body.updated_at = toUnix(body.updated_at); const headers = new Headers(res.headers); headers.set("middleware", "value"); return new Response(JSON.stringify(body), { @@ -558,8 +564,8 @@ describe("client", () => { const { data, response } = await client.GET("/self"); // assert body was modified - expect(data?.created_at).toBe(new Date(rawBody.created_at).getTime()); - expect(data?.updated_at).toBe(new Date(rawBody.updated_at).getTime()); + expect(data?.created_at).toBe(toUnix(rawBody.created_at)); + expect(data?.updated_at).toBe(toUnix(rawBody.updated_at)); // assert rest of body was preserved expect(data?.email).toBe(rawBody.email); // assert status changed @@ -700,6 +706,8 @@ describe("client", () => { }); it("can be ejected", async () => { + mockFetchOnce({ status: 200, body: "{}" }); + let called = false; const errorMiddleware = { onRequest() { @@ -759,7 +767,8 @@ describe("client", () => { // expect post_id to be encoded properly const req = fetchMocker.mock.calls[0][0]; - expect(req.body).toBeInstanceOf(FormData); + // note: this is FormData, but Node.js doesn’t handle new Request() properly with formData bodies. So this is only in tests. + expect(req.body).toBeInstanceOf(Buffer); // TODO: `vitest-fetch-mock` does not add the boundary to the Content-Type header like browsers do, so we expect the header to be null instead expect((req.headers as Headers).get("Content-Type")).toBeNull();