When discussing routes in terms of route handlers, one option is to utilize the node-mocks-http package for testing purposes. This package allows you to simulate a request sent to an endpoint. Here's an example:
// @vitest-environment node
import { expect, test } from "vitest";
import { createRequest } from "node-mocks-http";
import { GET } from "app/api/users/route";
test("User endpoint works as expected", async () => {
const source = "/tests/login.test.ts";
const nextUrl = new URL(source, process.env.HOST);
const request = createRequest({ method: "GET", url: "/api/users" });
const response = await GET({ ...request, nextUrl } as any);
expect(response.status).toBe(200);
// ...
});
In the above test code snippet, we demonstrate creating a simulated HTTP request and passing it directly to the route handler function without the need for an actual HTTP request.