-
I found this library via some online discussions. Looking through it, the only point of it is that it removes some boilerplates that you would have with e.g. import { RequestListener, createServer } from "http";
import { NextApiHandler } from "next";
import { apiResolver } from "next/dist/server/api-utils/node/api-resolver";
import request from "supertest";
import handler from "src/pages/api/hello-world";
const testClient = (handler: NextApiHandler) => {
const listener: RequestListener = (req, res) => {
return apiResolver(
req,
res,
undefined,
handler,
{
previewModeEncryptionKey: "",
previewModeId: "",
previewModeSigningKey: "",
},
false,
);
};
return request(createServer(listener));
};
test("API responds with 'hello-world'", async () => {
const client = testClient(handler);
const response = await client.get("/api/hello-world").expect(200);
expect(response.body.message).toBe("Hello World!");
}); So, what does |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hey @larscyl! I actually saw your comment on Reddit (or, at least, I think it was you), but for some reason I could not interact with that post. A few things: First, note how you're using the old Pages Router API resolver. The new App Router API resolver is much more involved than just calling a function, including, for instance: patching the global fetch function, supporting the magic helper functions via Second, the thing about relying on internal APIs like Additionally, note how your use of In terms of performance, NTARH uses a lightweight |
Beta Was this translation helpful? Give feedback.
Hey @larscyl! I actually saw your comment on Reddit (or, at least, I think it was you), but for some reason I could not interact with that post.
A few things:
First, note how you're using the old Pages Router API resolver. The new App Router API resolver is much more involved than just calling a function, including, for instance: patching the global fetch function, supporting the magic helper functions via
AsyncLocalStorage
, and passing around a properNextRequest
object.Second, the thing about relying on internal APIs like
AppRouteRouteModule
(app router) orapiResolver
(pages router) is that Vercel makes no guarantees about these primitives. If you take a peek at NTARH's source, you'll…