francis.miko
francis.miko
TTCTheo's Typesafe Cult
Created by francis.miko on 8/31/2023 in #questions
How does next13's /app routing api integrate Jest testing?
My test code looks like this:
describe(...
it(...
const mockInput = {...}
const mockRequest = {
method: "POST",
body: mockInput,
} as unknown as Request;

const mockResponse = {
status: jest.fn(),
json: jest.fn(),
} as unknown as Response;

await POST(mockRequest, mockResponse);

expect(mockResponse.status).toHaveBeenCalledWith(200);
expect(mockResponse.json).toHaveBeenCalledWith(expect.anything());
)
)
describe(...
it(...
const mockInput = {...}
const mockRequest = {
method: "POST",
body: mockInput,
} as unknown as Request;

const mockResponse = {
status: jest.fn(),
json: jest.fn(),
} as unknown as Response;

await POST(mockRequest, mockResponse);

expect(mockResponse.status).toHaveBeenCalledWith(200);
expect(mockResponse.json).toHaveBeenCalledWith(expect.anything());
)
)
And the types passed in by my POST are the native Request and Response types. The result is this error: expect(jest.fn()).toHaveBeenCalledWith(...expected) Expected: 200 Number of calls: 0 Looks like the request is not being impersonated to run? thus not getting any response.
3 replies
TTCTheo's Typesafe Cult
Created by francis.miko on 8/13/2023 in #questions
How Next.js 13 middleware is used as error handling?
8 replies
TTCTheo's Typesafe Cult
Created by francis.miko on 8/13/2023 in #questions
How Next.js 13 middleware is used as error handling?
I don't want to add a try catch module to each of my api codes. Doing so would defeat my original intention of using middleware to catch errors. So do you guys have any good insights? blessjmg
8 replies
TTCTheo's Typesafe Cult
Created by francis.miko on 8/13/2023 in #questions
How Next.js 13 middleware is used as error handling?
But this doesn't work, I can't catch related errors thrown by my api, for example my POST api:
const inputSchema = z.object({
message: z.string(),
});

export async function POST(req: Request) {
const body = await req.json();
const input = inputSchema.parse(body); // (input invalid parmas) => throws ZodError

return new Response(JSON.stringify({ status: "ok" }));
}
const inputSchema = z.object({
message: z.string(),
});

export async function POST(req: Request) {
const body = await req.json();
const input = inputSchema.parse(body); // (input invalid parmas) => throws ZodError

return new Response(JSON.stringify({ status: "ok" }));
}
8 replies
TTCTheo's Typesafe Cult
Created by francis.miko on 8/13/2023 in #questions
How Next.js 13 middleware is used as error handling?
Here is my existing code
export function middleware(request: Request) {
try {
const apiKey = request.headers.get("API_KEY");
if (apiKey !== env.API_KEY) {
return new Response(JSON.stringify({ error: "Invalid API key" }), {
status: 401,
});
}
return NextResponse.next();
} catch (err) {
if (error instanceof z.ZodError) {
return new Response(JSON.stringify(error.issues), { status: 422 });
}

return new Response(JSON.stringify(error), { status: 500 });
}
}
export function middleware(request: Request) {
try {
const apiKey = request.headers.get("API_KEY");
if (apiKey !== env.API_KEY) {
return new Response(JSON.stringify({ error: "Invalid API key" }), {
status: 401,
});
}
return NextResponse.next();
} catch (err) {
if (error instanceof z.ZodError) {
return new Response(JSON.stringify(error.issues), { status: 422 });
}

return new Response(JSON.stringify(error), { status: 500 });
}
}
8 replies