Skip to content

Commit c837fcd

Browse files
committed
Implement ESM + Wrangler approach for Cloudflare Workers testing
- Replace complex test approaches with simple ESM + Wrangler solution - Create test worker that runs Jest-style tests in Cloudflare Workers environment - Use ESM (native Cloudflare Workers module system) instead of CommonJS - Run tests in actual Cloudflare Workers runtime using wrangler dev - Avoid mime-db CommonJS compatibility issues by using ESM - Test optional types work correctly in Cloudflare Workers context - Add comprehensive GitHub Actions workflows for automated testing - Update documentation to reflect new ESM approach This approach is much cleaner and more accurate than previous methods, testing our SDK in the exact environment where users will run it.
1 parent b3d8e72 commit c837fcd

File tree

10 files changed

+382
-577
lines changed

10 files changed

+382
-577
lines changed

.github/workflows/README.md

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,39 @@ This directory contains GitHub Actions workflows for testing the Nylas Node.js S
44

55
## Workflows
66

7-
### `cloudflare-simple-test.yml`
8-
**Recommended approach** - Simple and effective Cloudflare Workers testing:
9-
- Uses Cloudflare's `nodejs_compat` environment to test our existing SDK
10-
- Runs compatibility tests locally without requiring Cloudflare deployment
11-
- Tests both CommonJS and ESM builds
7+
### `cloudflare-simple-test.yml` & `cloudflare-esm-test.yml`
8+
**Recommended approach** - ESM + Wrangler testing:
9+
- Uses ESM (ECMAScript Modules) for better Cloudflare Workers compatibility
10+
- Runs our normal test suites in actual Cloudflare Workers environment using Wrangler
11+
- Tests locally using `wrangler dev` to simulate production environment
1212
- Validates optional types work correctly in Cloudflare Workers context
1313
- Optional deployment testing (requires secrets)
1414

15-
### `cloudflare-nodejs-compat-test.yml`
16-
More comprehensive testing using Cloudflare Workers:
17-
- Creates a test worker that runs SDK tests in `nodejs_compat` environment
18-
- Tests locally using `wrangler dev`
19-
- Validates SDK functionality in actual Cloudflare Workers runtime
20-
21-
### `cloudflare-vitest-test.yml`
22-
Advanced testing using Cloudflare's Vitest integration:
23-
- Uses `@cloudflare/vitest-pool-workers` for integration testing
24-
- Runs tests directly in Cloudflare Workers context
25-
- More sophisticated testing setup
26-
2715
## Why This Approach Works
2816

29-
### **Cloudflare `nodejs_compat` Environment**
30-
- Cloudflare Workers supports Node.js compatibility through the `nodejs_compat` flag
31-
- This allows us to run our existing Node.js code (including the Nylas SDK) in Cloudflare Workers
32-
- We can test the exact same code that users will run in production
17+
### **ESM + Wrangler Environment**
18+
- Uses ESM which is the native module system for Cloudflare Workers
19+
- Runs tests in actual Cloudflare Workers runtime using Wrangler
20+
- Tests the exact same code that users will run in production
21+
- Avoids CommonJS compatibility issues (like mime-db problems)
3322

3423
### **Testing Optional Types**
3524
The main issue we're addressing is ensuring optional types work correctly in Cloudflare Workers. Our tests verify:
36-
- SDK can be imported in Cloudflare Workers context
25+
- SDK can be imported in Cloudflare Workers context using ESM
3726
- Client can be created with minimal configuration (tests optional types)
3827
- All optional properties work without TypeScript errors
39-
- Both CommonJS and ESM builds are compatible
28+
- ESM builds are fully compatible with Cloudflare Workers
4029

4130
## Local Testing
4231

4332
You can test Cloudflare Workers compatibility locally:
4433

4534
```bash
46-
# Run the compatibility test
35+
# Run the ESM + Wrangler test
4736
npm run test:cloudflare
4837

4938
# Or run the test script directly
50-
node test-cloudflare-compat.js
39+
node run-tests-cloudflare.mjs
5140
```
5241

5342
## GitHub Actions Setup
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
name: Cloudflare Workers ESM Test
2+
3+
on:
4+
push:
5+
branches:
6+
- cursor/add-cloudflare-worker-to-test-matrix-3aca
7+
- main
8+
pull_request:
9+
branches:
10+
- main
11+
workflow_dispatch:
12+
13+
jobs:
14+
test-in-cloudflare-workers:
15+
name: Test in Cloudflare Workers with ESM
16+
runs-on: ubuntu-latest
17+
steps:
18+
- name: Checkout code
19+
uses: actions/checkout@v4
20+
21+
- name: Setup Node.js
22+
uses: actions/setup-node@v4
23+
with:
24+
node-version: '20'
25+
cache: 'npm'
26+
27+
- name: Install dependencies
28+
run: npm ci
29+
30+
- name: Install Wrangler CLI
31+
run: npm install -g wrangler@latest
32+
33+
- name: Build the SDK
34+
run: npm run build
35+
36+
- name: Run tests in Cloudflare Workers environment
37+
run: |
38+
echo "🧪 Running Nylas SDK tests in Cloudflare Workers environment..."
39+
node run-tests-cloudflare.mjs
40+
41+
- name: Test with wrangler deploy --dry-run
42+
run: |
43+
cd cloudflare-test-worker
44+
echo "🔍 Testing worker build and deployment readiness..."
45+
wrangler deploy --dry-run
46+
echo "✅ Worker is ready for deployment"
47+
48+
# Optional: Deploy and test in actual Cloudflare environment
49+
deploy-and-test:
50+
name: Deploy and Test in Cloudflare
51+
runs-on: ubuntu-latest
52+
if: github.ref == 'refs/heads/main' && github.event_name == 'push' && secrets.CLOUDFLARE_API_TOKEN != ''
53+
needs: test-in-cloudflare-workers
54+
steps:
55+
- name: Checkout code
56+
uses: actions/checkout@v4
57+
58+
- name: Setup Node.js
59+
uses: actions/setup-node@v4
60+
with:
61+
node-version: '20'
62+
cache: 'npm'
63+
64+
- name: Install dependencies
65+
run: npm ci
66+
67+
- name: Build the SDK
68+
run: npm run build
69+
70+
- name: Deploy test worker to Cloudflare
71+
uses: cloudflare/wrangler-action@v3
72+
with:
73+
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
74+
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
75+
command: deploy
76+
workingDirectory: cloudflare-test-worker
77+
78+
- name: Test deployed worker
79+
run: |
80+
# Wait for deployment
81+
sleep 30
82+
83+
# Get worker URL
84+
WORKER_URL=$(cd cloudflare-test-worker && npx wrangler whoami --format json | jq -r '.subdomain')
85+
echo "Testing worker at: https://${WORKER_URL}.workers.dev"
86+
87+
# Run tests against deployed worker
88+
curl -f "https://${WORKER_URL}.workers.dev/test" | jq .

0 commit comments

Comments
 (0)