Skip to content

Commit 6b576a8

Browse files
committed
Fix tests
1 parent f98b87b commit 6b576a8

File tree

5 files changed

+30
-21
lines changed

5 files changed

+30
-21
lines changed

packages/openapi-fetch/src/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,9 @@ export default function createClient(clientOptions) {
7474
requestInit,
7575
);
7676
// remove `Content-Type` if serialized body is FormData; browser will correctly set Content-Type & boundary expression
77-
if (request.body instanceof FormData) {
77+
if (requestInit.body instanceof FormData) {
7878
request.headers.delete("Content-Type");
7979
}
80-
8180
// middleware (request)
8281
const mergedOptions = {
8382
baseUrl,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,10 @@ export interface components {
743743
email: string;
744744
age?: number;
745745
avatar?: string;
746+
/** Format: date */
747+
created_at: number;
748+
/** Format: date */
749+
updated_at: number;
746750
};
747751
};
748752
responses: {

packages/openapi-fetch/test/index.test.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
22
// @ts-expect-error
33
import createFetchMock from "vitest-fetch-mock";
44
import createClient, {
5+
type Middleware,
56
type MiddlewareRequest,
67
type QuerySerializerOptions,
78
} from "../src/index.js";
@@ -733,6 +734,8 @@ describe("client", () => {
733734
});
734735

735736
it("can modify response", async () => {
737+
const toUnix = (date: string) => new Date(date).getTime();
738+
736739
const rawBody = {
737740
738741
created_at: "2024-01-01T00:00:00Z",
@@ -746,10 +749,11 @@ describe("client", () => {
746749

747750
const client = createClient<paths>();
748751
client.use({
752+
// convert date string to unix time
749753
async onResponse(res) {
750754
const body = await res.json();
751-
body.created_at = new Date(body.created_at).getTime();
752-
body.updated_at = new Date(body.updated_at).getTime();
755+
body.created_at = toUnix(body.created_at);
756+
body.updated_at = toUnix(body.updated_at);
753757
const headers = new Headers(res.headers);
754758
headers.set("middleware", "value");
755759
return new Response(JSON.stringify(body), {
@@ -763,8 +767,8 @@ describe("client", () => {
763767
const { data, response } = await client.GET("/self");
764768

765769
// assert body was modified
766-
expect(data?.created_at).toBe(new Date(rawBody.created_at).getTime());
767-
expect(data?.updated_at).toBe(new Date(rawBody.updated_at).getTime());
770+
expect(data?.created_at).toBe(toUnix(rawBody.created_at));
771+
expect(data?.updated_at).toBe(toUnix(rawBody.updated_at));
768772
// assert rest of body was preserved
769773
expect(data?.email).toBe(rawBody.email);
770774
// assert status changed
@@ -905,6 +909,8 @@ describe("client", () => {
905909
});
906910

907911
it("can be ejected", async () => {
912+
mockFetchOnce({ status: 200, body: "{}" });
913+
908914
let called = false;
909915
const errorMiddleware = {
910916
onRequest() {
@@ -948,10 +954,11 @@ describe("client", () => {
948954

949955
// expect post_id to be encoded properly
950956
const req = fetchMocker.mock.calls[0][0];
951-
expect(req.body).toBeInstanceOf(FormData);
957+
// note: this is FormData, but Node.js doesn’t handle new Request() properly with formData bodies. So this is only in tests.
958+
expect(req.body).toBeInstanceOf(Buffer);
952959

953960
// 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
954-
expect((req.headers as Headers).get("Content-Type")).toBeNull();
961+
expect(req.headers.get("Content-Type")).toBeNull();
955962
});
956963

957964
it("respects cookie", async () => {

packages/openapi-fetch/test/v7-beta.test.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
22
// @ts-expect-error
33
import createFetchMock from "vitest-fetch-mock";
44
import createClient, {
5+
type Middleware,
56
type MiddlewareRequest,
67
type QuerySerializerOptions,
78
} from "../src/index.js";
@@ -725,6 +726,8 @@ describe("client", () => {
725726
});
726727

727728
it("can modify response", async () => {
729+
const toUnix = (date: string) => new Date(date).getTime();
730+
728731
const rawBody = {
729732
730733
created_at: "2024-01-01T00:00:00Z",
@@ -738,10 +741,11 @@ describe("client", () => {
738741

739742
const client = createClient<paths>();
740743
client.use({
744+
// convert date string to unix time
741745
async onResponse(res) {
742746
const body = await res.json();
743-
body.created_at = new Date(body.created_at).getTime();
744-
body.updated_at = new Date(body.updated_at).getTime();
747+
body.created_at = toUnix(body.created_at);
748+
body.updated_at = toUnix(body.updated_at);
745749
const headers = new Headers(res.headers);
746750
headers.set("middleware", "value");
747751
return new Response(JSON.stringify(body), {
@@ -755,8 +759,8 @@ describe("client", () => {
755759
const { data, response } = await client.GET("/self");
756760

757761
// assert body was modified
758-
expect(data?.created_at).toBe(new Date(rawBody.created_at).getTime());
759-
expect(data?.updated_at).toBe(new Date(rawBody.updated_at).getTime());
762+
expect(data?.created_at).toBe(toUnix(rawBody.created_at));
763+
expect(data?.updated_at).toBe(toUnix(rawBody.updated_at));
760764
// assert rest of body was preserved
761765
expect(data?.email).toBe(rawBody.email);
762766
// assert status changed
@@ -897,6 +901,8 @@ describe("client", () => {
897901
});
898902

899903
it("can be ejected", async () => {
904+
mockFetchOnce({ status: 200, body: "{}" });
905+
900906
let called = false;
901907
const errorMiddleware = {
902908
onRequest() {
@@ -939,7 +945,8 @@ describe("client", () => {
939945

940946
// expect post_id to be encoded properly
941947
const req = fetchMocker.mock.calls[0][0];
942-
expect(req.body).toBeInstanceOf(FormData);
948+
// note: this is FormData, but Node.js doesn’t handle new Request() properly with formData bodies. So this is only in tests.
949+
expect(req.body).toBeInstanceOf(Buffer);
943950

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

pnpm-lock.yaml

Lines changed: 0 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)