Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Next.js App Router - PostHog Tests

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
test:
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: lts/*

- uses: pnpm/action-setup@v4
with:
version: latest

- name: Install dependencies
run: |
cd basics/next-app-router
pnpm install
- name: Install Playwright Browsers
run: |
cd basics/next-app-router
pnpm exec playwright install chromium --with-deps
- name: Run Playwright tests
run: |
cd basics/next-app-router
pnpm exec playwright test
env:
NEXT_PUBLIC_POSTHOG_KEY: ${{ vars.NEXT_PUBLIC_POSTHOG_KEY }}
NEXT_PUBLIC_POSTHOG_HOST: ${{ vars.NEXT_PUBLIC_POSTHOG_HOST }}

- uses: actions/upload-artifact@v4
if: always()
with:
name: playwright-report
path: basics/next-app-router/playwright-report/
retention-days: 30
5 changes: 5 additions & 0 deletions basics/next-app-router/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# PostHog Configuration
# Get your PostHog API key from: https://app.posthog.com/project/settings
NEXT_PUBLIC_POSTHOG_KEY=your_posthog_project_api_key_here
# NEXT_PUBLIC_POSTHOG_HOST=https://eu.i.posthog.com
NEXT_PUBLIC_POSTHOG_HOST=https://us.i.posthog.com
6 changes: 5 additions & 1 deletion basics/next-app-router/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

# testing
/coverage
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/

# next.js
/.next/
Expand All @@ -31,7 +35,7 @@ yarn-error.log*
.pnpm-debug.log*

# env files (can opt-in for committing if needed)
.env*
.env
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.env.example is useful to allow


# vercel
.vercel
Expand Down
5 changes: 5 additions & 0 deletions basics/next-app-router/instrumentation-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
capture_exceptions: true,
// Turn on debug in development mode
debug: process.env.NODE_ENV === "development",
// @ignoreBlockStart
// Disable request batching in test environment
request_batching: false,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really suspect. I plan on updating this to detect if we're in CI and disable batching + allow bot agents with environment variables.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One wrinkle here is that we want to keep this kind of artifact (instrumentation-client.ts) specifically as close to what you'd want in user code as possible, so the robot can cheerfully and safely copy it.

could we do some sort of rewrite of it only during testing, or alter PostHog's behavior through a property set at runtime during tests?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh derp I imagine that's what you mean by your remarks about environment variables, carry on!

opt_out_useragent_filter: true, // This disables bot detection
// @ignoreBlockEnd
});
8 changes: 7 additions & 1 deletion basics/next-app-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
"dev": "next dev --turbopack",
"build": "next build --turbopack",
"start": "next start",
"lint": "eslint"
"lint": "eslint",
"postinstall": "playwright install chromium",
"test": "playwright test",
"test:ui": "playwright test --ui",
"test:report": "playwright show-report"
},
"dependencies": {
"next": "15.5.2",
Expand All @@ -17,9 +21,11 @@
},
"devDependencies": {
"@eslint/eslintrc": "^3",
"@playwright/test": "^1.56.1",
"@types/node": "^20",
"@types/react": "^19",
"@types/react-dom": "^19",
"dotenv": "^17.2.3",
"eslint": "^9",
"eslint-config-next": "15.5.2",
"typescript": "^5"
Expand Down
56 changes: 56 additions & 0 deletions basics/next-app-router/playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// @ignoreFile
import { defineConfig, devices } from '@playwright/test';
import { config } from 'dotenv';

// Load environment variables from .env file
config();

/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: './tests',
/* 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://127.0.0.1:3333',

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

/* Global timeout settings */
timeout: 180000, // 3 minutes for individual tests
expect: {
timeout: 60000, // 1 minute for expect assertions
},

/* Configure projects for major browsers */
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
snapshotPathTemplate: '{testDir}/{testFileName}-snapshots/{arg}{ext}',
},
],

/* Run your local dev server before starting the tests */
webServer: {
command: 'pnpm dev --port 3333',
url: 'http://127.0.0.1:3333',
reuseExistingServer: false, // Always start fresh
},
});


52 changes: 50 additions & 2 deletions basics/next-app-router/pnpm-lock.yaml

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

1 change: 1 addition & 0 deletions basics/next-app-router/src/lib/posthog-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export function getPostHogClient() {
flushInterval: 0
}
);
posthogClient.debug(true);
}
return posthogClient;
}
Expand Down
Loading