Skip to content

Commit 9887d0c

Browse files
authored
Merge pull request #2 from PostHog/tests
tests?
2 parents 3841fd5 + 4902048 commit 9887d0c

File tree

17 files changed

+876
-9
lines changed

17 files changed

+876
-9
lines changed

.github/workflows/playwright.yml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
name: PostHog Examples - Playwright Tests
2+
permissions:
3+
contents: read
4+
5+
on:
6+
push:
7+
branches: [ main ]
8+
pull_request:
9+
branches: [ main ]
10+
schedule:
11+
# Runs nightly at 12am PST (which is 8am UTC)
12+
- cron: '0 8 * * *'
13+
14+
jobs:
15+
discover:
16+
runs-on: ubuntu-latest
17+
outputs:
18+
examples: ${{ steps.set-examples.outputs.examples }}
19+
steps:
20+
- uses: actions/checkout@v4
21+
22+
- name: Discover examples with Playwright configs
23+
id: set-examples
24+
run: |
25+
examples=$(find basics -maxdepth 2 -name "playwright.config.ts" -exec dirname {} \; | sed 's|^basics/||' | jq -R -s -c 'split("\n")[:-1]')
26+
echo "examples=$examples" >> $GITHUB_OUTPUT
27+
echo "Found examples: $examples"
28+
29+
test:
30+
needs: discover
31+
if: needs.discover.outputs.examples != '[]'
32+
timeout-minutes: 60
33+
runs-on: ubuntu-latest
34+
strategy:
35+
fail-fast: false
36+
matrix:
37+
example: ${{ fromJson(needs.discover.outputs.examples) }}
38+
steps:
39+
- uses: actions/checkout@v4
40+
41+
- uses: actions/setup-node@v4
42+
with:
43+
node-version: lts/*
44+
45+
- uses: pnpm/action-setup@v4
46+
with:
47+
version: latest
48+
49+
- name: Install dependencies
50+
run: |
51+
cd basics/${{ matrix.example }}
52+
pnpm install
53+
54+
- name: Install Playwright Browsers
55+
run: |
56+
cd basics/${{ matrix.example }}
57+
pnpm exec playwright install chromium --with-deps
58+
59+
- name: Run Playwright tests
60+
run: |
61+
cd basics/${{ matrix.example }}
62+
pnpm exec playwright test
63+
env:
64+
NEXT_PUBLIC_POSTHOG_KEY: ${{ vars.NEXT_PUBLIC_POSTHOG_KEY }}
65+
NEXT_PUBLIC_POSTHOG_HOST: ${{ vars.NEXT_PUBLIC_POSTHOG_HOST }}
66+
67+
- uses: actions/upload-artifact@v4
68+
if: always()
69+
with:
70+
name: playwright-report-${{ matrix.example }}
71+
path: basics/${{ matrix.example }}/playwright-report/
72+
retention-days: 30
73+
74+
# TODO: report to PostHog which will warn in channel for failure.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# PostHog Configuration
2+
# Get your PostHog API key from: https://app.posthog.com/project/settings
3+
NEXT_PUBLIC_POSTHOG_KEY=your_posthog_project_api_key_here
4+
# NEXT_PUBLIC_POSTHOG_HOST=https://eu.i.posthog.com
5+
NEXT_PUBLIC_POSTHOG_HOST=https://us.i.posthog.com

basics/next-app-router/.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
# testing
1414
/coverage
15+
/test-results/
16+
/playwright-report/
17+
/blob-report/
18+
/playwright/.cache/
1519

1620
# next.js
1721
/.next/
@@ -31,7 +35,7 @@ yarn-error.log*
3135
.pnpm-debug.log*
3236

3337
# env files (can opt-in for committing if needed)
34-
.env*
38+
.env
3539

3640
# vercel
3741
.vercel

basics/next-app-router/instrumentation-client.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,9 @@ posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
99
capture_exceptions: true,
1010
// Turn on debug in development mode
1111
debug: process.env.NODE_ENV === "development",
12+
// @ignoreBlockStart
13+
// Disable request batching in test environment
14+
request_batching: false,
15+
opt_out_useragent_filter: true, // This disables bot detection
16+
// @ignoreBlockEnd
1217
});

basics/next-app-router/package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
"dev": "next dev --turbopack",
77
"build": "next build --turbopack",
88
"start": "next start",
9-
"lint": "eslint"
9+
"lint": "eslint",
10+
"postinstall": "playwright install chromium",
11+
"test": "playwright test",
12+
"test:ui": "playwright test --ui",
13+
"test:report": "playwright show-report"
1014
},
1115
"dependencies": {
1216
"next": "15.5.2",
@@ -17,9 +21,11 @@
1721
},
1822
"devDependencies": {
1923
"@eslint/eslintrc": "^3",
24+
"@playwright/test": "^1.56.1",
2025
"@types/node": "^20",
2126
"@types/react": "^19",
2227
"@types/react-dom": "^19",
28+
"dotenv": "^17.2.3",
2329
"eslint": "^9",
2430
"eslint-config-next": "15.5.2",
2531
"typescript": "^5"
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// @ignoreFile
2+
import { defineConfig, devices } from '@playwright/test';
3+
import { config } from 'dotenv';
4+
5+
// Load environment variables from .env file
6+
config();
7+
8+
/**
9+
* See https://playwright.dev/docs/test-configuration.
10+
*/
11+
export default defineConfig({
12+
testDir: './tests',
13+
/* Run tests in files in parallel */
14+
fullyParallel: true,
15+
/* Fail the build on CI if you accidentally left test.only in the source code. */
16+
forbidOnly: !!process.env.CI,
17+
/* Retry on CI only */
18+
retries: process.env.CI ? 2 : 0,
19+
/* Opt out of parallel tests on CI. */
20+
workers: process.env.CI ? 1 : undefined,
21+
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
22+
reporter: 'html',
23+
24+
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
25+
use: {
26+
/* Base URL to use in actions like `await page.goto('/')`. */
27+
baseURL: 'http://127.0.0.1:3333',
28+
29+
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
30+
trace: 'on-first-retry',
31+
},
32+
33+
/* Global timeout settings */
34+
timeout: 180000, // 3 minutes for individual tests
35+
expect: {
36+
timeout: 60000, // 1 minute for expect assertions
37+
},
38+
39+
/* Configure projects for major browsers */
40+
projects: [
41+
{
42+
name: 'chromium',
43+
use: { ...devices['Desktop Chrome'] },
44+
snapshotPathTemplate: '{testDir}/{testFileName}-snapshots/{arg}{ext}',
45+
},
46+
],
47+
48+
/* Run your local dev server before starting the tests */
49+
webServer: {
50+
command: 'pnpm dev --port 3333',
51+
url: 'http://127.0.0.1:3333',
52+
reuseExistingServer: false, // Always start fresh
53+
},
54+
});
55+
56+

basics/next-app-router/pnpm-lock.yaml

Lines changed: 50 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

basics/next-app-router/src/lib/posthog-server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export function getPostHogClient() {
1212
flushInterval: 0
1313
}
1414
);
15+
posthogClient.debug(true);
1516
}
1617
return posthogClient;
1718
}

0 commit comments

Comments
 (0)