test(coach-prefs): fix NextRequest URL mock — v1.4.22 carryover

The two failing cases in tests/integration/coach-prefs.test.ts were invoking
the route's GET handler with zero arguments — `(GET as () => Promise<Response>)()`.
The apiHandler wrapper at src/lib/api-handler.ts:46-47 reads args[0].url to
build the Wide Event `http.path`, so the call threw
`TypeError: Cannot read properties of undefined (reading 'url')` before the
route body ever ran. Other integration tests already pass a real Request to
GET (see analytics-health-score, chart-overlay-prefs); aligning this test to
the same pattern restores parity with production where Next.js always
invokes route handlers with a Request.

The api-handler wrapper itself was left untouched — defending against a
caller passing undefined would mask the same bug in any future test.
This commit is contained in:
Marc-André Bombeck committed 2026-05-14 11:36:27 +02:00
1 parent ac54bb26e8
commit 10dd2fc9ad
1 file changed
+6 -2
+6 -2
View File
@@ -70,7 +70,9 @@ describe("GET /api/auth/me/coach-prefs", () => {
await seedSession("coach-prefs-defaults");
const { GET } = await import("@/app/api/auth/me/coach-prefs/route");
const res = await (GET as () => Promise<Response>)();
const res = await (GET as (r: Request) => Promise<Response>)(
new Request("http://localhost/api/auth/me/coach-prefs"),
);
expect(res.status).toBe(200);
const env = (await res.json()) as { data: typeof DEFAULT_COACH_PREFS };
expect(env.data).toEqual(DEFAULT_COACH_PREFS);
@@ -108,7 +110,9 @@ describe("PUT /api/auth/me/coach-prefs", () => {
expect(row?.coachPrefsJson).toEqual(body);
// GET reads the saved values back.
const getRes = await (GET as () => Promise<Response>)();
const getRes = await (GET as (r: Request) => Promise<Response>)(
new Request("http://localhost/api/auth/me/coach-prefs"),
);
expect(getRes.status).toBe(200);
const getEnv = (await getRes.json()) as { data: typeof body };
expect(getEnv.data).toEqual(body);