Skip to content

Commit 130bd92

Browse files
committed
fix(test-utils): Fix TypeScript errors in spotlight.ts and event-proxy-server.ts
- Add null check for portMatch[1] in spotlight.ts - Add null check for eventBuffer element in generator - Refactor buffer pruning loop to avoid 'possibly undefined' error
1 parent 1c05b4c commit 130bd92

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed

.cursor/plans/migrate_e2e_to_spotlight_358ad839.plan.md

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,50 +65,43 @@ flowchart LR
6565
1. **Add `@spotlightjs/spotlight` dependency** to [`dev-packages/test-utils/package.json`](dev-packages/test-utils/package.json)
6666

6767
2. **Create new Spotlight-based helpers** in [`dev-packages/test-utils/src/spotlight.ts`](dev-packages/test-utils/src/spotlight.ts):
68-
6968
- `startSpotlight(options)` - Spawns `spotlight run` with dynamic port, parses port from stderr
7069
- `waitForSpotlightEvent(filter)` - Reads from Spotlight's JSON output stream
7170
- `waitForTransaction()`, `waitForError()`, etc. - High-level wrappers matching current API
7271

7372
3. **Update [`dev-packages/test-utils/src/playwright-config.ts`](dev-packages/test-utils/src/playwright-config.ts)**:
74-
7573
- Add option to use Spotlight instead of event proxy
7674
- Configure webServer to run Spotlight with dynamic port
7775
- Pass port via environment variable to test app
7876

7977
### Phase 2: Create Migration Path
8078

8179
4. **Design backward-compatible API**:
82-
8380
- Keep existing `waitForTransaction()` signature
8481
- New implementation reads from Spotlight instead of HTTP streaming
8582
- Port is passed via `SPOTLIGHT_PORT` env var
8683

8784
5. **Update DSN handling in test apps**:
88-
8985
- Replace `tunnel: 'http://localhost:3031'` with DSN workaround
9086
- DSN format: `http://spotlight@localhost:${SPOTLIGHT_PORT}/0`
9187
- This makes SDK operate in "normal" mode (no tunnel)
9288

9389
### Phase 3: Migrate Test Apps (Incremental)
9490

9591
6. **Migration per test app involves**:
96-
9792
- Remove `start-event-proxy.mjs`
9893
- Update `playwright.config.mjs` to use new Spotlight config
9994
- Update Sentry init to use DSN workaround instead of tunnel
10095
- No changes needed to test files (API stays the same)
10196

10297
7. **Start with problematic apps**:
103-
10498
- `react-router-7-lazy-routes` (the failing one)
10599
- `react-router-7-framework`
106100
- Then expand to remaining ~106 apps
107101

108102
### Phase 4: Cleanup
109103

110104
8. **After all apps migrated**:
111-
112105
- Remove old event proxy code from `event-proxy-server.ts`
113106
- Update documentation
114107
- Remove unused dependencies
@@ -129,7 +122,7 @@ Per [Spotlight docs](https://spotlightjs.com/docs/cli/run/#unsupported-sdks-dsn-
129122

130123
```javascript
131124
Sentry.init({
132-
dsn: `http://spotlight@localhost:${process.env.SPOTLIGHT_PORT}/0`
125+
dsn: `http://spotlight@localhost:${process.env.SPOTLIGHT_PORT}/0`,
133126
});
134127
```
135128

@@ -139,18 +132,16 @@ Sentry.init({
139132
// Before
140133
webServer: [
141134
{ command: 'node start-event-proxy.mjs', port: 3031 },
142-
{ command: 'yarn start', port: 3030 }
143-
]
135+
{ command: 'yarn start', port: 3030 },
136+
];
144137

145138
// After - spotlight run auto-detects and runs the app
146-
webServer: [
147-
{ command: 'yarn spotlight run -p 0 -f json', port: 3030 }
148-
]
139+
webServer: [{ command: 'yarn spotlight run -p 0 -f json', port: 3030 }];
149140
```
150141

151142
Note: `spotlight run` automatically:
152143

153144
- Detects the start script from package.json (`dev`, `develop`, `serve`, `start`)
154145
- Starts the Spotlight sidecar on the specified port (or dynamic with `-p 0`)
155146
- Streams events to stdout in JSON format (with `-f json`)
156-
- Sets `SENTRY_SPOTLIGHT` env var for the child process
147+
- Sets `SENTRY_SPOTLIGHT` env var for the child process

dev-packages/test-utils/src/event-proxy-server.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,13 @@ export async function startProxyServer(
153153
// Prune old events from the buffer that are definitely stale (strictly older than this listener).
154154
// Use strict < to match the live event behavior (>= at line 103) - same-ms events are kept.
155155
// This prevents memory buildup and ensures old events can never leak to future listeners.
156-
while (eventBuffer.length > 0 && eventBuffer[0].timestamp < listenerTimestamp) {
157-
eventBuffer.shift();
156+
while (eventBuffer.length > 0) {
157+
const firstEvent = eventBuffer[0];
158+
if (firstEvent && firstEvent.timestamp < listenerTimestamp) {
159+
eventBuffer.shift();
160+
} else {
161+
break;
162+
}
158163
}
159164

160165
// Send any remaining buffered events (those that arrived after this listener was registered,

dev-packages/test-utils/src/spotlight.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export async function startSpotlight(options: SpotlightOptions = {}): Promise<Sp
8181

8282
// Look for port in various formats
8383
const portMatch = line.match(/localhost:(\d+)/i) || line.match(/port[:\s]+(\d+)/i);
84-
if (portMatch && !resolvedPort) {
84+
if (portMatch?.[1] && !resolvedPort) {
8585
resolvedPort = parseInt(portMatch[1], 10);
8686
}
8787

@@ -156,7 +156,10 @@ function createSpotlightInstance(process: ChildProcess, port: number, _debug: bo
156156
while (true) {
157157
// Yield any buffered events first
158158
while (bufferIndex < eventBuffer.length) {
159-
yield eventBuffer[bufferIndex++];
159+
const envelope = eventBuffer[bufferIndex++];
160+
if (envelope) {
161+
yield envelope;
162+
}
160163
}
161164

162165
// Wait for new events

0 commit comments

Comments
 (0)