Skip to content

Commit d93241f

Browse files
JPeer264claude
andauthored
fix(bun): Use namespace imports for node:http in bunHttpServerIntegration (#24584)
closes #24580 closes [JS-3746](https://linear.app/getsentry/issue/JS-3746) The default imports of `node:http` and `node:https` compiled to `http.default.Server` in the CJS build. Bun's `node:http` has no `default` property under `require()`, so `Sentry.init()` threw for every CJS user. Adds a CJS scenario to the Bun integration tests. The Bun runner now accepts a scenario file path, like the Node runner. Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
1 parent fd85225 commit d93241f

4 files changed

Lines changed: 58 additions & 4 deletions

File tree

‎dev-packages/bun-integration-tests/runner.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Envelope, EnvelopeItemType } from '@sentry/core';
22
import { normalize } from '@sentry/core';
33
import { createBasicSentryServer } from '@sentry-internal/test-utils';
44
import { spawn } from 'child_process';
5-
import { existsSync } from 'fs';
5+
import { existsSync, statSync } from 'fs';
66
import { join } from 'path';
77
import { inspect } from 'util';
88
import { expect } from 'vitest';
@@ -63,6 +63,8 @@ export function createRunner(...paths: string[]) {
6363
throw new Error(`Test scenario not found: ${testPath}`);
6464
}
6565

66+
const entryFile = statSync(testPath).isDirectory() ? join(testPath, 'index.ts') : testPath;
67+
6668
const expectedEnvelopes: Expected[] = [];
6769
const ignored: Set<EnvelopeItemType> = new Set(['session', 'sessions', 'client_report']);
6870
const envVars: Record<string, string> = {};
@@ -170,7 +172,6 @@ export function createRunner(...paths: string[]) {
170172

171173
if (process.env.DEBUG) log('Starting scenario', testPath);
172174

173-
const entryFile = join(testPath, 'index.ts');
174175
if (!existsSync(entryFile)) {
175176
reject(new Error(`Entry file not found: ${entryFile}`));
176177
return;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const Sentry = require('@sentry/bun');
2+
3+
Sentry.init({
4+
dsn: process.env.SENTRY_DSN,
5+
tracesSampleRate: 1.0,
6+
});
7+
8+
const server = Bun.serve({
9+
port: 0,
10+
fetch() {
11+
throw new Error('This is a test error from a CommonJS Bun app');
12+
},
13+
error() {
14+
return new Response('Internal Server Error', { status: 500 });
15+
},
16+
});
17+
18+
process.send?.(JSON.stringify({ event: 'READY', port: server.port }));
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { expect, it } from 'vitest';
2+
import { eventEnvelope } from '../../expect';
3+
import { createRunner } from '../../runner';
4+
5+
it('initializes when @sentry/bun is loaded with require()', async ({ signal }) => {
6+
const runner = createRunner(__dirname, 'index.cjs')
7+
.expect(
8+
eventEnvelope(
9+
{
10+
level: 'error',
11+
exception: {
12+
values: [
13+
{
14+
type: 'Error',
15+
value: 'This is a test error from a CommonJS Bun app',
16+
stacktrace: {
17+
frames: expect.any(Array),
18+
},
19+
mechanism: { type: 'auto.http.bun.serve', handled: false },
20+
},
21+
],
22+
},
23+
request: expect.objectContaining({
24+
method: 'GET',
25+
url: expect.stringContaining('/error'),
26+
}),
27+
},
28+
{ includeSampleRand: true, includeTransaction: false },
29+
),
30+
)
31+
.ignore('span')
32+
.start(signal);
33+
await runner.makeRequest('get', '/error', { expectError: true });
34+
await runner.completed();
35+
});

‎packages/bun/src/integrations/bunHttpServer.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { errorMonitor } from 'node:events';
2-
import http from 'node:http';
3-
import https from 'node:https';
2+
import * as http from 'node:http';
3+
import * as https from 'node:https';
44
import type { IntegrationFn, Span } from '@sentry/core';
55
import { defineIntegration } from '@sentry/core';
66
import type { HttpIncomingMessage, HttpServerResponse } from '@sentry/core/server';

0 commit comments

Comments
 (0)