Skip to content

Commit db8969b

Browse files
authored
test: describe playwright tests (#402)
1 parent 624d75a commit db8969b

File tree

10 files changed

+146
-126
lines changed

10 files changed

+146
-126
lines changed

examples/bugs/gh-119/e2e/base.spec.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { test, expect } from "@playwright/test";
22

3-
test("the index page of the application shows the Next.js logo", async ({ page }) => {
4-
await page.goto("/");
5-
await expect(page.getByAltText("Next.js logo")).toBeVisible();
3+
test.describe("bugs/gh-119", () => {
4+
test("the index page of the application shows the Next.js logo", async ({ page }) => {
5+
await page.goto("/");
6+
await expect(page.getByAltText("Next.js logo")).toBeVisible();
7+
});
68
});

examples/bugs/gh-219/e2e/base.spec.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { test, expect } from "@playwright/test";
22

3-
test("the index page of the application shows the Next.js logo", async ({ page }) => {
4-
await page.goto("/");
5-
await expect(page.getByAltText("Next.js logo")).toBeVisible();
3+
test.describe("bugs/gh-219", () => {
4+
test("the index page of the application shows the Next.js logo", async ({ page }) => {
5+
await page.goto("/");
6+
await expect(page.getByAltText("Next.js logo")).toBeVisible();
7+
});
68
});

examples/bugs/gh-223/e2e/base.spec.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { test, expect } from "@playwright/test";
22

3-
test("api route", async ({ page }) => {
4-
const res = await page.request.get("/api/image");
5-
expect(res.status()).toEqual(200);
6-
expect((await res.json()).image).toEqual("");
3+
test.describe("bugs/gh-223", () => {
4+
test("api route", async ({ page }) => {
5+
const res = await page.request.get("/api/image");
6+
expect(res.status()).toEqual(200);
7+
expect((await res.json()).image).toEqual("");
8+
});
79
});
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { test, expect } from "@playwright/test";
22

3-
test("the index page of the application shows the Next.js logo", async ({ page }) => {
4-
await page.goto("/");
5-
await expect(page.getByAltText("Next.js logo")).toBeVisible();
3+
test.describe("create-next-app", () => {
4+
test("the index page of the application shows the Next.js logo", async ({ page }) => {
5+
await page.goto("/");
6+
await expect(page.getByAltText("Next.js logo")).toBeVisible();
7+
});
68
});

examples/middleware/e2e/base.spec.ts

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
11
import { test, expect } from "@playwright/test";
22

3-
test("redirect", async ({ page }) => {
4-
await page.goto("/");
5-
await page.click('[href="/about"]');
6-
await page.waitForURL("**/redirected");
7-
expect(await page.textContent("h1")).toContain("Redirected");
8-
});
3+
test.describe("middleware", () => {
4+
test("redirect", async ({ page }) => {
5+
await page.goto("/");
6+
await page.click('[href="/about"]');
7+
await page.waitForURL("**/redirected");
8+
expect(await page.textContent("h1")).toContain("Redirected");
9+
});
910

10-
test("rewrite", async ({ page }) => {
11-
await page.goto("/");
12-
await page.click('[href="/another"]');
13-
await page.waitForURL("**/another");
14-
expect(await page.textContent("h1")).toContain("Rewrite");
15-
});
11+
test("rewrite", async ({ page }) => {
12+
await page.goto("/");
13+
await page.click('[href="/another"]');
14+
await page.waitForURL("**/another");
15+
expect(await page.textContent("h1")).toContain("Rewrite");
16+
});
1617

17-
test("no matching middleware", async ({ page }) => {
18-
await page.goto("/");
19-
await page.click('[href="/about2"]');
20-
await page.waitForURL("**/about2");
21-
expect(await page.textContent("h1")).toContain("About 2");
22-
});
18+
test("no matching middleware", async ({ page }) => {
19+
await page.goto("/");
20+
await page.click('[href="/about2"]');
21+
await page.waitForURL("**/about2");
22+
expect(await page.textContent("h1")).toContain("About 2");
23+
});
2324

24-
test("matching noop middleware", async ({ page }) => {
25-
await page.goto("/");
26-
await page.click('[href="/middleware"]');
27-
await page.waitForURL("**/middleware");
28-
expect(await page.textContent("h1")).toContain("Via middleware");
29-
});
25+
test("matching noop middleware", async ({ page }) => {
26+
await page.goto("/");
27+
await page.click('[href="/middleware"]');
28+
await page.waitForURL("**/middleware");
29+
expect(await page.textContent("h1")).toContain("Via middleware");
30+
});
3031

31-
// Test for https://github.com/opennextjs/opennextjs-cloudflare/issues/201
32-
test("clerk middleware", async ({ page }) => {
33-
const res = await page.request.post("/clerk", { data: "some body" });
34-
expect(res.ok()).toEqual(true);
35-
expect(res.status()).toEqual(200);
36-
await expect(res.text()).resolves.toEqual("Hello clerk");
32+
// Test for https://github.com/opennextjs/opennextjs-cloudflare/issues/201
33+
test("clerk middleware", async ({ page }) => {
34+
const res = await page.request.post("/clerk", { data: "some body" });
35+
expect(res.ok()).toEqual(true);
36+
expect(res.status()).toEqual(200);
37+
await expect(res.text()).resolves.toEqual("Hello clerk");
38+
});
3739
});
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { test, expect } from "@playwright/test";
22

3-
test("middlewares have access to the cloudflare context", async ({ page }) => {
4-
await page.goto("/middleware");
5-
const cloudflareContextHeaderElement = page.getByTestId("cloudflare-context-header");
6-
expect(await cloudflareContextHeaderElement.textContent()).toContain(
7-
"typeof `cloudflareContext.env` = object"
8-
);
3+
test.describe("middleware/cloudflare-context", () => {
4+
test("middlewares have access to the cloudflare context", async ({ page }) => {
5+
await page.goto("/middleware");
6+
const cloudflareContextHeaderElement = page.getByTestId("cloudflare-context-header");
7+
expect(await cloudflareContextHeaderElement.textContent()).toContain(
8+
"typeof `cloudflareContext.env` = object"
9+
);
10+
});
911
});

examples/playground/e2e/base.spec.ts

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,50 @@ export function validateMd5(data: Buffer, expectedHash: string) {
1212
);
1313
}
1414

15-
test("index", async ({ page }) => {
16-
await page.goto("/");
17-
await expect(page.getByText("Test misc Next features")).toBeVisible();
18-
});
15+
test.describe("playground/base", () => {
16+
test("index", async ({ page }) => {
17+
await page.goto("/");
18+
await expect(page.getByText("Test misc Next features")).toBeVisible();
19+
});
1920

20-
test("the hello-world api GET route works as intended", async ({ page }) => {
21-
const res = await page.request.get("/api/hello");
22-
expect(res.headers()["content-type"]).toContain("text/plain");
23-
expect(await res.text()).toEqual("Hello World!");
24-
});
21+
test("the hello-world api GET route works as intended", async ({ page }) => {
22+
const res = await page.request.get("/api/hello");
23+
expect(res.headers()["content-type"]).toContain("text/plain");
24+
expect(await res.text()).toEqual("Hello World!");
25+
});
2526

26-
test("returns a hello world string from the cloudflare context env", async ({ page }) => {
27-
const res = await page.request.get("/api/hello", {
28-
headers: {
29-
"from-cloudflare-context": "true",
30-
},
27+
test("returns a hello world string from the cloudflare context env", async ({ page }) => {
28+
const res = await page.request.get("/api/hello", {
29+
headers: {
30+
"from-cloudflare-context": "true",
31+
},
32+
});
33+
expect(res.headers()["content-type"]).toContain("text/plain");
34+
expect(await res.text()).toEqual("Hello World from the cloudflare context!");
3135
});
32-
expect(res.headers()["content-type"]).toContain("text/plain");
33-
expect(await res.text()).toEqual("Hello World from the cloudflare context!");
34-
});
3536

36-
test("the hello-world api POST route works as intended", async ({ page }) => {
37-
const res = await page.request.post("/api/hello", { data: "some body" });
38-
expect(res.headers()["content-type"]).toContain("text/plain");
39-
await expect(res.text()).resolves.toEqual("Hello post-World! body=some body");
40-
});
37+
test("the hello-world api POST route works as intended", async ({ page }) => {
38+
const res = await page.request.post("/api/hello", { data: "some body" });
39+
expect(res.headers()["content-type"]).toContain("text/plain");
40+
await expect(res.text()).resolves.toEqual("Hello post-World! body=some body");
41+
});
4142

42-
test("sets environment variables from the Next.js env file", async ({ page }) => {
43-
const res = await page.request.get("/api/env");
44-
await expect(res.json()).resolves.toEqual(expect.objectContaining({ TEST_ENV_VAR: "TEST_VALUE" }));
45-
});
43+
test("sets environment variables from the Next.js env file", async ({ page }) => {
44+
const res = await page.request.get("/api/env");
45+
await expect(res.json()).resolves.toEqual(expect.objectContaining({ TEST_ENV_VAR: "TEST_VALUE" }));
46+
});
4647

47-
test("returns correct information about the request from a route handler", async ({ page }) => {
48-
const res = await page.request.get("/api/request");
49-
// Next.js can fall back to `localhost:3000` or `n` if it doesn't get the host - neither of these are expected.
50-
const expectedURL = expect.stringMatching(/https?:\/\/localhost:(?!3000)\d+\/api\/request/);
51-
await expect(res.json()).resolves.toEqual({ nextUrl: expectedURL, url: expectedURL });
52-
});
48+
test("returns correct information about the request from a route handler", async ({ page }) => {
49+
const res = await page.request.get("/api/request");
50+
// Next.js can fall back to `localhost:3000` or `n` if it doesn't get the host - neither of these are expected.
51+
const expectedURL = expect.stringMatching(/https?:\/\/localhost:(?!3000)\d+\/api\/request/);
52+
await expect(res.json()).resolves.toEqual({ nextUrl: expectedURL, url: expectedURL });
53+
});
5354

54-
test("generates an og image successfully", async ({ page }) => {
55-
const res = await page.request.get("/og");
56-
expect(res.status()).toEqual(200);
57-
expect(res.headers()["content-type"]).toEqual("image/png");
58-
expect(validateMd5(await res.body(), OG_MD5)).toEqual(true);
55+
test("generates an og image successfully", async ({ page }) => {
56+
const res = await page.request.get("/og");
57+
expect(res.status()).toEqual(200);
58+
expect(res.headers()["content-type"]).toEqual("image/png");
59+
expect(validateMd5(await res.body(), OG_MD5)).toEqual(true);
60+
});
5961
});

examples/playground/e2e/cloudflare.spec.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66

77
import { test, expect } from "@playwright/test";
88

9-
test("NextConfig", async ({ page }) => {
10-
const res = await page.request.get("/api/buildid");
11-
expect(res.status()).toEqual(200);
12-
const { nextConfig } = await res.json();
13-
expect(nextConfig.output).toEqual("standalone");
9+
test.describe("playground/cloudflare", () => {
10+
test("NextConfig", async ({ page }) => {
11+
const res = await page.request.get("/api/buildid");
12+
expect(res.status()).toEqual(200);
13+
const { nextConfig } = await res.json();
14+
expect(nextConfig.output).toEqual("standalone");
15+
});
1416
});

examples/playground/e2e/isr.spec.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,31 @@ import { test, expect, type APIResponse } from "@playwright/test";
22
import type { BinaryLike } from "node:crypto";
33
import { createHash } from "node:crypto";
44

5-
test("Generated pages exist", async ({ page }) => {
6-
const generatedIds = [1, 2, 3];
7-
let res: APIResponse;
8-
for (const id of generatedIds) {
9-
res = await page.request.get(`/isr/${id}/dynamic`);
10-
expect(res.status()).toBe(200);
11-
res = await page.request.get(`/isr/${id}/no-dynamic`);
12-
expect(res.status()).toBe(200);
13-
}
14-
});
5+
test.describe("playground/isr", () => {
6+
test("Generated pages exist", async ({ page }) => {
7+
const generatedIds = [1, 2, 3];
8+
let res: APIResponse;
9+
for (const id of generatedIds) {
10+
res = await page.request.get(`/isr/${id}/dynamic`);
11+
expect(res.status()).toBe(200);
12+
res = await page.request.get(`/isr/${id}/no-dynamic`);
13+
expect(res.status()).toBe(200);
14+
}
15+
});
1516

16-
test("Non generated pages 404 when dynamic is false", async ({ page }) => {
17-
const generatedIds = [4, 5, 6];
18-
for (const id of generatedIds) {
19-
const res = await page.request.get(`/isr/${id}/no-dynamic`);
20-
expect(res.status()).toBe(404);
21-
}
22-
});
17+
test("Non generated pages 404 when dynamic is false", async ({ page }) => {
18+
const generatedIds = [4, 5, 6];
19+
for (const id of generatedIds) {
20+
const res = await page.request.get(`/isr/${id}/no-dynamic`);
21+
expect(res.status()).toBe(404);
22+
}
23+
});
2324

24-
test("Non generated pages are generated when dynamic is true", async ({ page }) => {
25-
const generatedIds = [4, 5, 6];
26-
for (const id of generatedIds) {
27-
const res = await page.request.get(`/isr/${id}/dynamic`);
28-
expect(res.status()).toBe(200);
29-
}
25+
test("Non generated pages are generated when dynamic is true", async ({ page }) => {
26+
const generatedIds = [4, 5, 6];
27+
for (const id of generatedIds) {
28+
const res = await page.request.get(`/isr/${id}/dynamic`);
29+
expect(res.status()).toBe(200);
30+
}
31+
});
3032
});

examples/ssg-app/e2e/base.spec.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import { test, expect } from "@playwright/test";
22

3-
test("the index page should work", async ({ page }) => {
4-
await page.goto("/");
5-
await expect(page.getByText("Hello from a Statically generated page")).toBeVisible();
6-
});
3+
test.describe("ssg-app", () => {
4+
test("the index page should work", async ({ page }) => {
5+
await page.goto("/");
6+
await expect(page.getByText("Hello from a Statically generated page")).toBeVisible();
7+
});
78

8-
test("the APP_VERSION var from the cloudflare context should be displayed", async ({ page }) => {
9-
await page.goto("/");
10-
await expect(page.getByTestId("app-version")).toHaveText("1.2.345");
11-
});
9+
test("the APP_VERSION var from the cloudflare context should be displayed", async ({ page }) => {
10+
await page.goto("/");
11+
await expect(page.getByTestId("app-version")).toHaveText("1.2.345");
12+
});
1213

13-
// Note: secrets from .dev.vars are also part of the SSG output, this is expected and nothing we can avoid
14-
test("the MY_SECRET secret from the cloudflare context should be displayed", async ({ page }) => {
15-
await page.goto("/");
16-
await expect(page.getByTestId("my-secret")).toHaveText("psst... this is a secret!");
14+
// Note: secrets from .dev.vars are also part of the SSG output, this is expected and nothing we can avoid
15+
test("the MY_SECRET secret from the cloudflare context should be displayed", async ({ page }) => {
16+
await page.goto("/");
17+
await expect(page.getByTestId("my-secret")).toHaveText("psst... this is a secret!");
18+
});
1719
});

0 commit comments

Comments
 (0)