Skip to content

Commit 3f1f585

Browse files
MichaelGHSegclaude
andauthored
Add E2E CLIs for Node and Browser (#1343)
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 6567607 commit 3f1f585

14 files changed

Lines changed: 830 additions & 0 deletions

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# E2E Tests for analytics-next (Browser SDK)
2+
# Copy this file to: analytics-next/.github/workflows/e2e-browser-tests.yml
3+
#
4+
# This workflow:
5+
# 1. Checks out the SDK and sdk-e2e-tests repos
6+
# 2. Applies HTTP patch to allow mock server testing
7+
# 3. Builds the SDK monorepo and browser e2e-cli
8+
# 4. Runs the e2e test suite in both standard and batching modes
9+
10+
name: E2E Tests (Browser)
11+
12+
on:
13+
push:
14+
branches: [main, master]
15+
pull_request:
16+
branches: [main, master]
17+
workflow_dispatch: # Allow manual trigger
18+
19+
jobs:
20+
e2e-tests:
21+
runs-on: ubuntu-latest
22+
strategy:
23+
matrix:
24+
mode:
25+
- name: standard
26+
env_batching: 'false'
27+
- name: batching
28+
env_batching: 'true'
29+
30+
name: Browser E2E (${{ matrix.mode.name }})
31+
32+
steps:
33+
- name: Checkout SDK
34+
uses: actions/checkout@v4
35+
with:
36+
path: sdk
37+
38+
- name: Checkout sdk-e2e-tests
39+
uses: actions/checkout@v4
40+
with:
41+
repository: segmentio/sdk-e2e-tests
42+
token: ${{ secrets.E2E_TESTS_TOKEN }}
43+
path: sdk-e2e-tests
44+
45+
- name: Setup Node.js
46+
uses: actions/setup-node@v4
47+
with:
48+
node-version: '20'
49+
50+
- name: Apply HTTP patch for testing
51+
working-directory: sdk
52+
run: |
53+
git apply ../sdk-e2e-tests/patches/analytics-browser-http.patch
54+
echo "HTTP patch applied successfully"
55+
56+
- name: Install SDK dependencies
57+
working-directory: sdk
58+
run: yarn install
59+
60+
- name: Build SDK packages
61+
working-directory: sdk
62+
run: yarn build
63+
64+
- name: Install and build browser e2e-cli
65+
working-directory: sdk/packages/browser/e2e-cli
66+
run: |
67+
npm install
68+
npm run build
69+
70+
- name: Run E2E tests
71+
working-directory: sdk-e2e-tests
72+
env:
73+
BROWSER_BATCHING: ${{ matrix.mode.env_batching }}
74+
run: |
75+
./scripts/run-tests.sh \
76+
--sdk-dir "${{ github.workspace }}/sdk/packages/browser/e2e-cli" \
77+
--cli "node ${{ github.workspace }}/sdk/packages/browser/e2e-cli/dist/cli.js" \
78+
--sdk-path "${{ github.workspace }}/sdk"
79+
80+
- name: Upload test results
81+
if: always()
82+
uses: actions/upload-artifact@v4
83+
with:
84+
name: e2e-test-results-browser-${{ matrix.mode.name }}
85+
path: sdk-e2e-tests/test-results/
86+
if-no-files-found: ignore

.github/workflows/e2e-tests.yml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# E2E Tests for analytics-next (Node.js SDK)
2+
# Copy this file to: analytics-next/.github/workflows/e2e-tests.yml
3+
#
4+
# This workflow:
5+
# 1. Checks out the SDK and sdk-e2e-tests repos
6+
# 2. Builds the SDK monorepo and e2e-cli
7+
# 3. Runs the e2e test suite
8+
9+
name: E2E Tests
10+
11+
on:
12+
push:
13+
branches: [main, master]
14+
pull_request:
15+
branches: [main, master]
16+
workflow_dispatch: # Allow manual trigger
17+
18+
jobs:
19+
e2e-tests:
20+
runs-on: ubuntu-latest
21+
22+
steps:
23+
- name: Checkout SDK
24+
uses: actions/checkout@v4
25+
with:
26+
path: sdk
27+
28+
- name: Checkout sdk-e2e-tests
29+
uses: actions/checkout@v4
30+
with:
31+
repository: segmentio/sdk-e2e-tests
32+
token: ${{ secrets.E2E_TESTS_TOKEN }}
33+
path: sdk-e2e-tests
34+
35+
- name: Setup Node.js
36+
uses: actions/setup-node@v4
37+
with:
38+
node-version: '20'
39+
40+
- name: Install SDK dependencies
41+
working-directory: sdk
42+
run: yarn install
43+
44+
- name: Build SDK packages
45+
working-directory: sdk
46+
run: yarn build
47+
48+
- name: Build e2e-cli
49+
working-directory: sdk/packages/node/e2e-cli
50+
run: |
51+
npm install
52+
npm run build
53+
54+
- name: Run E2E tests
55+
working-directory: sdk-e2e-tests
56+
run: |
57+
./scripts/run-tests.sh \
58+
--sdk-dir "${{ github.workspace }}/sdk/packages/node/e2e-cli" \
59+
--cli "node ${{ github.workspace }}/sdk/packages/node/e2e-cli/dist/cli.js"
60+
61+
- name: Upload test results
62+
if: always()
63+
uses: actions/upload-artifact@v4
64+
with:
65+
name: e2e-test-results
66+
path: sdk-e2e-tests/test-results/
67+
if-no-files-found: ignore

packages/browser/e2e-cli/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# analytics-browser e2e-cli
2+
3+
E2E test CLI for the [@segment/analytics-next](https://github.com/segmentio/analytics-next) browser SDK. Runs the browser SDK inside a jsdom environment, accepts a JSON input describing events and SDK configuration, and outputs results as JSON.
4+
5+
## Setup
6+
7+
```bash
8+
npm install
9+
npm run build
10+
```
11+
12+
## Usage
13+
14+
```bash
15+
node dist/cli.js --input '{"writeKey":"...", ...}'
16+
```
17+
18+
Supports batching mode via environment variable:
19+
20+
```bash
21+
BROWSER_BATCHING=true node dist/cli.js --input '...'
22+
```
23+
24+
## Input Format
25+
26+
```jsonc
27+
{
28+
"writeKey": "your-write-key", // required
29+
"apiHost": "https://...", // optional — SDK default if omitted
30+
"cdnHost": "https://...", // optional — SDK default if omitted
31+
"sequences": [ // required — event sequences to send
32+
{
33+
"delayMs": 0,
34+
"events": [
35+
{ "type": "track", "event": "Test", "userId": "user-1" }
36+
]
37+
}
38+
],
39+
"config": { // optional
40+
"flushAt": 1,
41+
"flushInterval": 1000,
42+
"maxRetries": 3,
43+
"timeout": 15
44+
}
45+
}
46+
```
47+
48+
## Output Format
49+
50+
```json
51+
{ "success": true, "sentBatches": 1 }
52+
```
53+
54+
On failure:
55+
56+
```json
57+
{ "success": false, "error": "description", "sentBatches": 0 }
58+
```
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"sdk": "browser",
3+
"test_suites": "basic",
4+
"auto_settings": true,
5+
"patch": "analytics-browser-http.patch",
6+
"env": {
7+
"BROWSER_BATCHING": "false"
8+
}
9+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "@segment/analytics-browser-e2e-cli",
3+
"version": "1.0.0",
4+
"private": true,
5+
"description": "E2E CLI for browser analytics SDK using jsdom",
6+
"main": "dist/cli.js",
7+
"scripts": {
8+
"build": "tsc",
9+
"start": "node dist/cli.js"
10+
},
11+
"dependencies": {
12+
"@segment/analytics-next": "file:..",
13+
"jsdom": "^19.0.0"
14+
},
15+
"devDependencies": {
16+
"@types/jsdom": "^16.2.14",
17+
"@types/node": "^18.0.0",
18+
"typescript": "^4.7.4"
19+
}
20+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/bash
2+
#
3+
# Run E2E tests for analytics-next (Browser SDK)
4+
#
5+
# Prerequisites: Node.js 18+, yarn
6+
#
7+
# Usage:
8+
# ./run-e2e.sh [extra args passed to run-tests.sh]
9+
#
10+
# Override sdk-e2e-tests location:
11+
# E2E_TESTS_DIR=../my-e2e-tests ./run-e2e.sh
12+
#
13+
14+
set -e
15+
16+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
17+
SDK_ROOT="$SCRIPT_DIR/../../.."
18+
E2E_DIR="${E2E_TESTS_DIR:-$SDK_ROOT/../sdk-e2e-tests}"
19+
20+
echo "=== Building analytics-next (Browser) e2e-cli ==="
21+
22+
# Apply HTTP patch (needed for browser SDK to use http:// URLs)
23+
cd "$SDK_ROOT"
24+
if git apply --check "$E2E_DIR/patches/analytics-browser-http.patch" 2>/dev/null; then
25+
git apply "$E2E_DIR/patches/analytics-browser-http.patch"
26+
echo "HTTP patch applied"
27+
else
28+
echo "HTTP patch already applied or not applicable (skipping)"
29+
fi
30+
31+
# Install and build SDK monorepo
32+
yarn install
33+
yarn build
34+
35+
# Build e2e-cli
36+
cd "$SCRIPT_DIR"
37+
npm install
38+
npm run build
39+
40+
echo ""
41+
42+
# Run tests
43+
cd "$E2E_DIR"
44+
./scripts/run-tests.sh \
45+
--sdk-dir "$SCRIPT_DIR" \
46+
--cli "node $SCRIPT_DIR/dist/cli.js" \
47+
--sdk-path "$SDK_ROOT" \
48+
"$@"

0 commit comments

Comments
 (0)