Skip to content

Commit ce5f868

Browse files
committed
Fix tests
1 parent f11e187 commit ce5f868

File tree

5 files changed

+33
-15
lines changed

5 files changed

+33
-15
lines changed

Diff for: packages/openapi-fetch/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"lint": "pnpm run \"/^lint:/\"",
5656
"lint:js": "eslint \"{src,test}/**/*.{js,ts}\"",
5757
"lint:prettier": "prettier --check \"{src,test}/**/*\"",
58-
"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",
58+
"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",
5959
"pretest": "pnpm run generate-types",
6060
"test": "pnpm run \"/^test:/\"",
6161
"test:js": "vitest run",

Diff for: packages/openapi-fetch/src/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,9 @@ export default function createClient(clientOptions) {
5656
requestInit,
5757
);
5858
// remove `Content-Type` if serialized body is FormData; browser will correctly set Content-Type & boundary expression
59-
if (request.body instanceof FormData) {
59+
if (requestInit.body instanceof FormData) {
6060
request.headers.delete("Content-Type");
6161
}
62-
6362
// middleware (request)
6463
const mergedOptions = {
6564
baseUrl,

Diff for: packages/openapi-fetch/test/fixtures/v7-beta.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,10 @@ export interface components {
645645
email: string;
646646
age?: number;
647647
avatar?: string;
648+
/** Format: date */
649+
created_at: number;
650+
/** Format: date */
651+
updated_at: number;
648652
};
649653
};
650654
responses: {

Diff for: packages/openapi-fetch/test/index.test.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,8 @@ describe("client", () => {
522522
});
523523

524524
it("can modify response", async () => {
525+
const toUnix = (date: string) => new Date(date).getTime();
526+
525527
const rawBody = {
526528
527529
created_at: "2024-01-01T00:00:00Z",
@@ -535,10 +537,11 @@ describe("client", () => {
535537

536538
const client = createClient<paths>();
537539
client.use({
540+
// convert date string to unix time
538541
async onResponse(res) {
539542
const body = await res.json();
540-
body.created_at = new Date(body.created_at).getTime();
541-
body.updated_at = new Date(body.updated_at).getTime();
543+
body.created_at = toUnix(body.created_at);
544+
body.updated_at = toUnix(body.updated_at);
542545
const headers = new Headers(res.headers);
543546
headers.set("middleware", "value");
544547
return new Response(JSON.stringify(body), {
@@ -552,8 +555,8 @@ describe("client", () => {
552555
const { data, response } = await client.GET("/self");
553556

554557
// assert body was modified
555-
expect(data?.created_at).toBe(new Date(rawBody.created_at).getTime());
556-
expect(data?.updated_at).toBe(new Date(rawBody.updated_at).getTime());
558+
expect(data?.created_at).toBe(toUnix(rawBody.created_at));
559+
expect(data?.updated_at).toBe(toUnix(rawBody.updated_at));
557560
// assert rest of body was preserved
558561
expect(data?.email).toBe(rawBody.email);
559562
// assert status changed
@@ -694,6 +697,8 @@ describe("client", () => {
694697
});
695698

696699
it("can be ejected", async () => {
700+
mockFetchOnce({ status: 200, body: "{}" });
701+
697702
let called = false;
698703
const errorMiddleware = {
699704
onRequest() {
@@ -754,10 +759,11 @@ describe("client", () => {
754759

755760
// expect post_id to be encoded properly
756761
const req = fetchMocker.mock.calls[0][0];
757-
expect(req.body).toBeInstanceOf(FormData);
762+
// note: this is FormData, but Node.js doesn’t handle new Request() properly with formData bodies. So this is only in tests.
763+
expect(req.body).toBeInstanceOf(Buffer);
758764

759765
// 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
760-
expect((req.headers as Headers).get("Content-Type")).toBeNull();
766+
expect(req.headers.get("Content-Type")).toBeNull();
761767
});
762768
});
763769

Diff for: packages/openapi-fetch/test/v7-beta.test.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
22
// @ts-expect-error
33
import createFetchMock from "vitest-fetch-mock";
4-
import createClient, { type Middleware } from "../src/index.js";
4+
import createClient, {
5+
type MiddlewareRequest,
6+
type Middleware,
7+
} from "../src/index.js";
58
import type { paths } from "./fixtures/v7-beta.js";
69

710
// Note
@@ -528,6 +531,8 @@ describe("client", () => {
528531
});
529532

530533
it("can modify response", async () => {
534+
const toUnix = (date: string) => new Date(date).getTime();
535+
531536
const rawBody = {
532537
533538
created_at: "2024-01-01T00:00:00Z",
@@ -541,10 +546,11 @@ describe("client", () => {
541546

542547
const client = createClient<paths>();
543548
client.use({
549+
// convert date string to unix time
544550
async onResponse(res) {
545551
const body = await res.json();
546-
body.created_at = new Date(body.created_at).getTime();
547-
body.updated_at = new Date(body.updated_at).getTime();
552+
body.created_at = toUnix(body.created_at);
553+
body.updated_at = toUnix(body.updated_at);
548554
const headers = new Headers(res.headers);
549555
headers.set("middleware", "value");
550556
return new Response(JSON.stringify(body), {
@@ -558,8 +564,8 @@ describe("client", () => {
558564
const { data, response } = await client.GET("/self");
559565

560566
// assert body was modified
561-
expect(data?.created_at).toBe(new Date(rawBody.created_at).getTime());
562-
expect(data?.updated_at).toBe(new Date(rawBody.updated_at).getTime());
567+
expect(data?.created_at).toBe(toUnix(rawBody.created_at));
568+
expect(data?.updated_at).toBe(toUnix(rawBody.updated_at));
563569
// assert rest of body was preserved
564570
expect(data?.email).toBe(rawBody.email);
565571
// assert status changed
@@ -700,6 +706,8 @@ describe("client", () => {
700706
});
701707

702708
it("can be ejected", async () => {
709+
mockFetchOnce({ status: 200, body: "{}" });
710+
703711
let called = false;
704712
const errorMiddleware = {
705713
onRequest() {
@@ -759,7 +767,8 @@ describe("client", () => {
759767

760768
// expect post_id to be encoded properly
761769
const req = fetchMocker.mock.calls[0][0];
762-
expect(req.body).toBeInstanceOf(FormData);
770+
// note: this is FormData, but Node.js doesn’t handle new Request() properly with formData bodies. So this is only in tests.
771+
expect(req.body).toBeInstanceOf(Buffer);
763772

764773
// 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
765774
expect((req.headers as Headers).get("Content-Type")).toBeNull();

0 commit comments

Comments
 (0)