Skip to content

Commit 972287e

Browse files
authored
add: e2e for api routes in pages router (#764)
* add e2e for api routes in pages router * add next issue link * lint
1 parent be7854e commit 972287e

File tree

6 files changed

+75
-2
lines changed

6 files changed

+75
-2
lines changed
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import type { NextApiRequest, NextApiResponse } from "next";
2+
3+
export default function handler(req: NextApiRequest, res: NextApiResponse) {
4+
const { slug } = req.query;
5+
res.status(200).json({ slug });
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { NextApiRequest, NextApiResponse } from "next";
2+
3+
export default function handler(req: NextApiRequest, res: NextApiResponse) {
4+
res.status(200).json({ optional: "true" });
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { NextApiRequest, NextApiResponse } from "next";
2+
3+
export default function handler(req: NextApiRequest, res: NextApiResponse) {
4+
const { slug } = req.query;
5+
if (!Array.isArray(slug)) {
6+
return res.status(500).json({ error: "Invalid" });
7+
}
8+
res.status(200).json({ slug });
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { NextApiRequest, NextApiResponse } from "next";
2+
3+
export default function handler(req: NextApiRequest, res: NextApiResponse) {
4+
res.status(200).json({ precedence: "true" });
5+
}

Diff for: examples/pages-router/src/pages/api/hello.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
1+
// Next.js API route support: https://nextjs.org/docs/pages/building-your-application/routing/api-routes
22
import type { NextApiRequest, NextApiResponse } from "next";
33

44
type Data = {
@@ -9,5 +9,5 @@ export default function handler(
99
req: NextApiRequest,
1010
res: NextApiResponse<Data>,
1111
) {
12-
res.status(200).json({ hello: "world" });
12+
res.status(200).json({ hello: "OpenNext rocks!" });
1313
}

Diff for: packages/tests-e2e/tests/pagesRouter/api.test.ts

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { expect, test } from "@playwright/test";
2+
3+
test("should not fail on an api route", async ({ page }) => {
4+
const result = await page.goto("/api/hello");
5+
expect(result?.status()).toBe(200);
6+
const body = await result?.json();
7+
expect(body).toEqual({ hello: "OpenNext rocks!" });
8+
});
9+
10+
test("should work with dynamic api route", async ({ page }) => {
11+
const result = await page.goto("/api/dynamic/opennext");
12+
expect(result?.status()).toBe(200);
13+
const body = await result?.json();
14+
expect(body).toEqual({ slug: "opennext" });
15+
});
16+
17+
test("should work with catch all api route", async ({ page }) => {
18+
const result = await page.goto("/api/dynamic/catch-all/first/second/third");
19+
expect(result?.status()).toBe(200);
20+
const body = await result?.json();
21+
expect(body).toEqual({ slug: ["first", "second", "third"] });
22+
});
23+
24+
test("dynamic route should take precedence over catch all", async ({
25+
page,
26+
}) => {
27+
const result = await page.goto("/api/dynamic/catch-all");
28+
expect(result?.status()).toBe(200);
29+
const body = await result?.json();
30+
expect(body).toEqual({ slug: "catch-all" });
31+
});
32+
33+
test("should work with optional catch all api route", async ({ page }) => {
34+
const result = await page.goto("/api/dynamic/catch-all-optional");
35+
expect(result?.status()).toBe(200);
36+
const body = await result?.json();
37+
expect(body).toEqual({ optional: "true" });
38+
});
39+
40+
// This should work but it doesn't. It seems that the predefined api route is not taking precedence.
41+
// Its broken in `next start` aswell.
42+
// Issue currently open in Next: https://github.com/vercel/next.js/issues/76765
43+
test.skip("predefined api route should take presedence", async ({ page }) => {
44+
const result = await page.goto("/api/dynamic/precedence");
45+
expect(result?.status()).toBe(200);
46+
const body = await result?.json();
47+
expect(body).toEqual({ precedence: "true" });
48+
});

0 commit comments

Comments
 (0)