I’m using Next.js version 13, and I’ve written a test for the backend endpoint. It’s been more than half a day, but I haven’t been able to figure out the error. I’ve searched a lot on Google, but couldn’t find a solution. The error I’m getting is that I created an endpoint and wrote a test for it using Jest, but as soon as I run npm run test, it says ReferenceError: Response is not defined. If anyone knows about this, please help me
// api/items/route
import { NextRequest, NextResponse } from "next/server";
const items = [
{ id: 1, name: "Item 1" },
{ id: 2, name: "Item 2" },
];
export async function POST(req: NextRequest) {
const requestBody = await req.json();
const item = {
id: items.length + 1,
name: requestBody.name,
};
items.push(item);
return NextResponse.json(item, { status: 201 });
}
item.test.ts
import { POST } from "../src/app/api/items/route";
it("should return added data with status 201", async () => {
const requestObj = {
json: async () => ({ name: "Item 3" }),
} as any;
const response = await POST(requestObj);
const body = await response.json();
expect(response.status).toBe(201);
expect(body.id).toEqual(expect.any(Number));
expect(body.name).toBe("Item 3");
});
this is error