Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
drwpow committed Jan 28, 2024
1 parent de70bd8 commit 77f2787
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/openapi-fetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 1 addition & 2 deletions packages/openapi-fetch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions packages/openapi-fetch/test/fixtures/v7-beta.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,10 @@ export interface components {
email: string;
age?: number;
avatar?: string;
/** Format: date */
created_at: number;
/** Format: date */
updated_at: number;
};
};
responses: {
Expand Down
18 changes: 12 additions & 6 deletions packages/openapi-fetch/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,8 @@ describe("client", () => {
});

it("can modify response", async () => {
const toUnix = (date: string) => new Date(date).getTime();

const rawBody = {
email: "[email protected]",
created_at: "2024-01-01T00:00:00Z",
Expand All @@ -535,10 +537,11 @@ describe("client", () => {

const client = createClient<paths>();
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), {
Expand All @@ -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
Expand Down Expand Up @@ -694,6 +697,8 @@ describe("client", () => {
});

it("can be ejected", async () => {
mockFetchOnce({ status: 200, body: "{}" });

let called = false;
const errorMiddleware = {
onRequest() {
Expand Down Expand Up @@ -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();
});
});

Expand Down
21 changes: 15 additions & 6 deletions packages/openapi-fetch/test/v7-beta.test.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -528,6 +531,8 @@ describe("client", () => {
});

it("can modify response", async () => {
const toUnix = (date: string) => new Date(date).getTime();

const rawBody = {
email: "[email protected]",
created_at: "2024-01-01T00:00:00Z",
Expand All @@ -541,10 +546,11 @@ describe("client", () => {

const client = createClient<paths>();
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), {
Expand All @@ -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
Expand Down Expand Up @@ -700,6 +706,8 @@ describe("client", () => {
});

it("can be ejected", async () => {
mockFetchOnce({ status: 200, body: "{}" });

let called = false;
const errorMiddleware = {
onRequest() {
Expand Down Expand Up @@ -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();
Expand Down
8 changes: 0 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 77f2787

Please sign in to comment.