Skip to content

Commit 469940b

Browse files
committed
fix tests
1 parent b6fda2b commit 469940b

File tree

5 files changed

+35
-27
lines changed

5 files changed

+35
-27
lines changed

dev-packages/e2e-tests/test-applications/SPOTLIGHT_ENV_TESTS.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Tests the **CJS build** scenario where `import.meta` must be stripped to avoid s
2121
**Source**: `nextjs-15/app/spotlight-env-test/page.tsx`
2222

2323
#### Tests:
24+
2425
1. **`respects NEXT_PUBLIC_SENTRY_SPOTLIGHT environment variable`**
2526
- Verifies that `NEXT_PUBLIC_SENTRY_SPOTLIGHT=true` enables Spotlight
2627
- Checks that the Spotlight integration is registered
@@ -39,6 +40,7 @@ Tests the **CJS build** scenario where `import.meta` must be stripped to avoid s
3940
- Monitors console for syntax errors
4041

4142
#### Environment Setup:
43+
4244
```bash
4345
NEXT_PUBLIC_SENTRY_SPOTLIGHT=true
4446
# SENTRY_SPOTLIGHT can be set for backend, but won't be exposed to browser
@@ -52,6 +54,7 @@ Tests the **ESM build** scenario where `import.meta` should be present and funct
5254
**Source**: `browser-webworker-vite/src/spotlight-env-test.ts`
5355

5456
#### Tests:
57+
5558
1. **`respects VITE_SENTRY_SPOTLIGHT environment variable`**
5659
- Verifies that `VITE_SENTRY_SPOTLIGHT=true` enables Spotlight
5760
- Checks that the Spotlight integration is registered
@@ -78,6 +81,7 @@ Tests the **ESM build** scenario where `import.meta` should be present and funct
7881
- Verifies it successfully reads from `import.meta.env`
7982

8083
#### Environment Setup:
84+
8185
```bash
8286
VITE_SENTRY_SPOTLIGHT=true
8387
```
@@ -119,6 +123,7 @@ try {
119123
```
120124

121125
This code is:
126+
122127
- **Included** in ESM builds (Vite, Astro, SvelteKit)
123128
- **Stripped** from CJS builds (Next.js, Webpack, etc.)
124129

@@ -130,25 +135,26 @@ The shared `resolveSpotlightOptions` function filters empty/whitespace strings:
130135

131136
```typescript
132137
// Treat empty/whitespace-only strings as undefined
133-
const envUrl = typeof envSpotlight === 'string' && envSpotlight.trim() !== ''
134-
? envSpotlight
135-
: undefined;
138+
const envUrl = typeof envSpotlight === 'string' && envSpotlight.trim() !== '' ? envSpotlight : undefined;
136139
```
137140

138141
This ensures:
142+
139143
- Empty strings never enable Spotlight
140144
- Whitespace-only strings are treated as undefined
141145
- No invalid URL connections are attempted
142146

143147
## Running the Tests
144148

145149
### Next.js Tests
150+
146151
```bash
147152
cd dev-packages/e2e-tests/test-applications/nextjs-15
148153
NEXT_PUBLIC_SENTRY_SPOTLIGHT=true pnpm test tests/spotlight-env.test.ts
149154
```
150155

151156
### Vite Tests
157+
152158
```bash
153159
cd dev-packages/e2e-tests/test-applications/browser-webworker-vite
154160
VITE_SENTRY_SPOTLIGHT=true pnpm test tests/spotlight-env.test.ts
@@ -157,13 +163,15 @@ VITE_SENTRY_SPOTLIGHT=true pnpm test tests/spotlight-env.test.ts
157163
## Expected Outcomes
158164

159165
### Next.js (CJS)
166+
160167
-`process.env.NEXT_PUBLIC_SENTRY_SPOTLIGHT` accessible
161168
-`SENTRY_SPOTLIGHT` NOT accessible (backend-only)
162169
- ✅ No `import.meta` syntax in output
163170
- ✅ No syntax errors
164171
- ✅ Spotlight integration enabled
165172

166173
### Vite (ESM)
174+
167175
-`import.meta.env.VITE_SENTRY_SPOTLIGHT` accessible
168176
-`process.env.VITE_SENTRY_SPOTLIGHT` accessible (transformed)
169177
-`import.meta` syntax present in output
@@ -173,16 +181,19 @@ VITE_SENTRY_SPOTLIGHT=true pnpm test tests/spotlight-env.test.ts
173181
## Troubleshooting
174182

175183
### Syntax Error: Cannot use import.meta outside a module
184+
176185
- **Cause**: `import.meta` code not stripped from CJS build
177186
- **Fix**: Verify `makeStripEsmPlugin()` is applied to CJS builds
178187
- **Check**: Look for `/* rollup-esm-only */` comments in source
179188

180189
### Spotlight not enabled despite env var set
190+
181191
- **Cause**: Empty string or wrong prefix
182192
- **Fix**: Use correct prefix (`NEXT_PUBLIC_*` for Next.js, `VITE_*` for Vite)
183193
- **Check**: Verify `resolveSpotlightOptions` receives non-empty string
184194

185195
### import.meta.env returns undefined in Vite
196+
186197
- **Cause**: `import.meta` code stripped from ESM build
187198
- **Fix**: Verify `makeStripEsmPlugin()` is NOT applied to ESM builds
188199
- **Check**: ESM builds should only use `makeStripCjsPlugin()`

dev-packages/e2e-tests/test-applications/browser-webworker-vite/tests/spotlight-env.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,8 @@ test.describe('Spotlight environment variable handling in Vite (ESM)', () => {
8383
await page.waitForTimeout(1000);
8484

8585
// Should have no syntax errors
86-
const syntaxErrors = [...consoleErrors, ...pageErrors].filter(err =>
87-
err.includes('SyntaxError') ||
88-
err.includes('Unexpected token') ||
89-
err.includes('Cannot use import.meta')
86+
const syntaxErrors = [...consoleErrors, ...pageErrors].filter(
87+
err => err.includes('SyntaxError') || err.includes('Unexpected token') || err.includes('Cannot use import.meta'),
9088
);
9189

9290
expect(syntaxErrors).toHaveLength(0);

dev-packages/e2e-tests/test-applications/nextjs-15/app/spotlight-env-test/page.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ export default function SpotlightEnvTestPage() {
3232
<div data-testid="next-public-spotlight">
3333
NEXT_PUBLIC_SENTRY_SPOTLIGHT: {envVars.NEXT_PUBLIC_SENTRY_SPOTLIGHT || 'undefined'}
3434
</div>
35-
<div data-testid="sentry-spotlight">
36-
SENTRY_SPOTLIGHT: {envVars.SENTRY_SPOTLIGHT || 'undefined'}
37-
</div>
35+
<div data-testid="sentry-spotlight">SENTRY_SPOTLIGHT: {envVars.SENTRY_SPOTLIGHT || 'undefined'}</div>
3836
</div>
3937
<div id="spotlight-status" data-testid="spotlight-status">
4038
<h2>Spotlight Integration Status</h2>

dev-packages/e2e-tests/test-applications/nextjs-15/tests/spotlight-env.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,8 @@ test.describe('Spotlight environment variable handling', () => {
5959
await page.waitForTimeout(1000);
6060

6161
// Should have no syntax errors from import.meta in CJS build
62-
const syntaxErrors = consoleErrors.filter(err =>
63-
err.includes('import.meta') ||
64-
err.includes('SyntaxError') ||
65-
err.includes('Unexpected token')
62+
const syntaxErrors = consoleErrors.filter(
63+
err => err.includes('import.meta') || err.includes('SyntaxError') || err.includes('Unexpected token'),
6664
);
6765

6866
expect(syntaxErrors).toHaveLength(0);

packages/browser/test/build-artifacts.test.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Tests to verify build artifacts are correctly formatted for their module type.
3-
*
3+
*
44
* These tests verify that:
55
* - CJS builds don't contain import.meta (would cause syntax errors)
66
* - ESM builds DO contain import.meta.env checks
@@ -22,21 +22,25 @@ describe('Build Artifacts - CJS vs ESM', () => {
2222

2323
if (fs.existsSync(cjsDevPath)) {
2424
const cjsDevContent = fs.readFileSync(cjsDevPath, 'utf-8');
25-
25+
2626
// Should NOT contain actual import.meta code (only in comments is ok)
27-
const lines = cjsDevContent.split('\n').filter(line => !line.trim().startsWith('//') && !line.trim().startsWith('*'));
27+
const lines = cjsDevContent
28+
.split('\n')
29+
.filter(line => !line.trim().startsWith('//') && !line.trim().startsWith('*'));
2830
const codeOnly = lines.join('\n');
29-
31+
3032
expect(codeOnly).not.toContain('import.meta.env');
3133
expect(codeOnly).not.toContain('typeof import.meta');
3234
}
3335

3436
if (fs.existsSync(cjsProdPath)) {
3537
const cjsProdContent = fs.readFileSync(cjsProdPath, 'utf-8');
36-
37-
const lines = cjsProdContent.split('\n').filter(line => !line.trim().startsWith('//') && !line.trim().startsWith('*'));
38+
39+
const lines = cjsProdContent
40+
.split('\n')
41+
.filter(line => !line.trim().startsWith('//') && !line.trim().startsWith('*'));
3842
const codeOnly = lines.join('\n');
39-
43+
4044
expect(codeOnly).not.toContain('import.meta.env');
4145
expect(codeOnly).not.toContain('typeof import.meta');
4246
}
@@ -47,7 +51,7 @@ describe('Build Artifacts - CJS vs ESM', () => {
4751

4852
if (fs.existsSync(cjsDevPath)) {
4953
const content = fs.readFileSync(cjsDevPath, 'utf-8');
50-
54+
5155
// SHOULD contain process.env check
5256
expect(content).toContain('process.env');
5357
}
@@ -61,15 +65,15 @@ describe('Build Artifacts - CJS vs ESM', () => {
6165

6266
if (fs.existsSync(esmDevPath)) {
6367
const content = fs.readFileSync(esmDevPath, 'utf-8');
64-
68+
6569
// SHOULD contain import.meta checks
6670
expect(content).toContain('import.meta');
6771
expect(content).toContain('import.meta.env');
6872
}
6973

7074
if (fs.existsSync(esmProdPath)) {
7175
const content = fs.readFileSync(esmProdPath, 'utf-8');
72-
76+
7377
expect(content).toContain('import.meta');
7478
expect(content).toContain('import.meta.env');
7579
}
@@ -80,7 +84,7 @@ describe('Build Artifacts - CJS vs ESM', () => {
8084

8185
if (fs.existsSync(esmDevPath)) {
8286
const content = fs.readFileSync(esmDevPath, 'utf-8');
83-
87+
8488
// SHOULD contain the rollup markers (they're kept in output as comments)
8589
expect(content).toContain('rollup-esm-only');
8690
}
@@ -91,7 +95,7 @@ describe('Build Artifacts - CJS vs ESM', () => {
9195

9296
if (fs.existsSync(esmDevPath)) {
9397
const content = fs.readFileSync(esmDevPath, 'utf-8');
94-
98+
9599
// SHOULD contain process.env check too (for compatibility)
96100
expect(content).toContain('process.env');
97101
}
@@ -115,4 +119,3 @@ describe('Build Artifacts - CJS vs ESM', () => {
115119
});
116120
});
117121
});
118-

0 commit comments

Comments
 (0)