Skip to content

Commit

Permalink
test: add middleware e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb committed Oct 30, 2024
1 parent 3af28ee commit 1debd9b
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 1 deletion.
6 changes: 6 additions & 0 deletions examples/middleware/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts

# playwright
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
22 changes: 22 additions & 0 deletions examples/middleware/e2e/base.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { test, expect } from "@playwright/test";

test("redirect", async ({ page }) => {
await page.goto("/");
await page.click('[href="/about"]');
expect(page.waitForURL("**/redirected"));
expect(await page.textContent("h1")).toContain("Redirected");
});

test("rewrite", async ({ page }) => {
await page.goto("/");
await page.click('[href="/another"]');
expect(page.waitForURL("**/another"));
expect(await page.textContent("h1")).toContain("Rewrite");
});

test("noop middleware", async ({ page }) => {
await page.goto("/");
await page.click('[href="/about2"]');
expect(page.waitForURL("**/about2"));
expect(await page.textContent("h1")).toContain("About 2");
});
53 changes: 53 additions & 0 deletions examples/middleware/e2e/playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { defineConfig, devices } from "@playwright/test";

declare const process: { env: Record<string, string> };

/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: "./",
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: "html",
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('/')`. */
baseURL: "http://localhost:8770",

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: "on-first-retry",
},

/* Configure projects for major browsers */
projects: [
{
name: "chromium",
use: { ...devices["Desktop Chrome"] },
},

{
name: "firefox",
use: { ...devices["Desktop Firefox"] },
},

{
name: "webkit",
use: { ...devices["Desktop Safari"] },
},
],

/* Run your local dev server before starting the tests */
webServer: {
command: "pnpm preview:worker",
url: "http://localhost:8770",
reuseExistingServer: !process.env.CI,
},
});
4 changes: 3 additions & 1 deletion examples/middleware/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"lint": "next lint",
"build:worker": "pnpm cloudflare",
"dev:worker": "wrangler dev --port 8770 --inspector-port 9330",
"preview:worker": "pnpm build:worker && pnpm dev:worker"
"preview:worker": "pnpm build:worker && pnpm dev:worker",
"e2e": "playwright test -c e2e/playwright.config.ts"
},
"dependencies": {
"next": "catalog:",
Expand All @@ -17,6 +18,7 @@
},
"devDependencies": {
"@opennextjs/cloudflare": "workspace:*",
"@playwright/test": "catalog:",
"@types/node": "18.0.0",
"@types/react": "catalog:",
"@types/react-dom": "catalog:",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1debd9b

Please sign in to comment.