Skip to content

Commit 61258d0

Browse files
committed
test: middleware (to increase coverage)
1 parent 9705397 commit 61258d0

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/core/definer.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable no-console */
22
import { afterEach, describe, expect, it, jest } from "@jest/globals";
33
import z from "zod";
4+
import type { RouteMethodHandler } from "~/types/next";
45
import defineRoute from "./definer";
56

67
describe("defineRoute", () => {
@@ -311,4 +312,37 @@ describe("defineRoute", () => {
311312
expect(response.status).toBe(500);
312313
expect(bodyText).toBe("Backend developer is gonna get fired");
313314
});
315+
316+
it("should apply middleware to the route handler", async () => {
317+
type Handler = RouteMethodHandler<unknown, Request, Response>;
318+
const mockMiddleware = jest.fn((handler: Handler) => async (request: Request, context?: { params: unknown }) => {
319+
await Promise.resolve();
320+
return handler(request, context);
321+
});
322+
323+
const route = defineRoute({
324+
operationId: "middlewareExample",
325+
method: "GET",
326+
summary: "Middleware Example",
327+
description: "Example route with middleware applied",
328+
tags: ["example"],
329+
action: (source, _request) => {
330+
// Confirm middleware effect in action
331+
expect(source).toHaveProperty("pathParams", null);
332+
return new Response("Middleware success", { status: 200 });
333+
},
334+
responses: {
335+
200: { description: "OK" },
336+
},
337+
middleware: mockMiddleware,
338+
});
339+
340+
const nextJsRouteHandler = route.GET;
341+
const response = await nextJsRouteHandler(mockRequest as unknown as Request);
342+
343+
expect(response).toBeInstanceOf(Response);
344+
expect(response.status).toBe(200);
345+
expect(await response.text()).toBe("Middleware success");
346+
expect(mockMiddleware).toHaveBeenCalled();
347+
});
314348
});

0 commit comments

Comments
 (0)