diff --git a/dev-packages/bun-integration-tests/runner.ts b/dev-packages/bun-integration-tests/runner.ts index 6115196213d2..51f9e4e7fa60 100644 --- a/dev-packages/bun-integration-tests/runner.ts +++ b/dev-packages/bun-integration-tests/runner.ts @@ -2,7 +2,7 @@ import type { Envelope, EnvelopeItemType } from '@sentry/core'; import { normalize } from '@sentry/core'; import { createBasicSentryServer } from '@sentry-internal/test-utils'; import { spawn } from 'child_process'; -import { existsSync } from 'fs'; +import { existsSync, statSync } from 'fs'; import { join } from 'path'; import { inspect } from 'util'; import { expect } from 'vitest'; @@ -63,6 +63,8 @@ export function createRunner(...paths: string[]) { throw new Error(`Test scenario not found: ${testPath}`); } + const entryFile = statSync(testPath).isDirectory() ? join(testPath, 'index.ts') : testPath; + const expectedEnvelopes: Expected[] = []; const ignored: Set = new Set(['session', 'sessions', 'client_report']); const envVars: Record = {}; @@ -170,7 +172,6 @@ export function createRunner(...paths: string[]) { if (process.env.DEBUG) log('Starting scenario', testPath); - const entryFile = join(testPath, 'index.ts'); if (!existsSync(entryFile)) { reject(new Error(`Entry file not found: ${entryFile}`)); return; diff --git a/dev-packages/bun-integration-tests/suites/cjs/index.cjs b/dev-packages/bun-integration-tests/suites/cjs/index.cjs new file mode 100644 index 000000000000..d21203678e20 --- /dev/null +++ b/dev-packages/bun-integration-tests/suites/cjs/index.cjs @@ -0,0 +1,18 @@ +const Sentry = require('@sentry/bun'); + +Sentry.init({ + dsn: process.env.SENTRY_DSN, + tracesSampleRate: 1.0, +}); + +const server = Bun.serve({ + port: 0, + fetch() { + throw new Error('This is a test error from a CommonJS Bun app'); + }, + error() { + return new Response('Internal Server Error', { status: 500 }); + }, +}); + +process.send?.(JSON.stringify({ event: 'READY', port: server.port })); diff --git a/dev-packages/bun-integration-tests/suites/cjs/test.ts b/dev-packages/bun-integration-tests/suites/cjs/test.ts new file mode 100644 index 000000000000..e70f0ddb8971 --- /dev/null +++ b/dev-packages/bun-integration-tests/suites/cjs/test.ts @@ -0,0 +1,35 @@ +import { expect, it } from 'vitest'; +import { eventEnvelope } from '../../expect'; +import { createRunner } from '../../runner'; + +it('initializes when @sentry/bun is loaded with require()', async ({ signal }) => { + const runner = createRunner(__dirname, 'index.cjs') + .expect( + eventEnvelope( + { + level: 'error', + exception: { + values: [ + { + type: 'Error', + value: 'This is a test error from a CommonJS Bun app', + stacktrace: { + frames: expect.any(Array), + }, + mechanism: { type: 'auto.http.bun.serve', handled: false }, + }, + ], + }, + request: expect.objectContaining({ + method: 'GET', + url: expect.stringContaining('/error'), + }), + }, + { includeSampleRand: true, includeTransaction: false }, + ), + ) + .ignore('span') + .start(signal); + await runner.makeRequest('get', '/error', { expectError: true }); + await runner.completed(); +}); diff --git a/packages/bun/src/integrations/bunHttpServer.ts b/packages/bun/src/integrations/bunHttpServer.ts index f3a671ed4fab..f21d47e93f67 100644 --- a/packages/bun/src/integrations/bunHttpServer.ts +++ b/packages/bun/src/integrations/bunHttpServer.ts @@ -1,6 +1,6 @@ import { errorMonitor } from 'node:events'; -import http from 'node:http'; -import https from 'node:https'; +import * as http from 'node:http'; +import * as https from 'node:https'; import type { IntegrationFn, Span } from '@sentry/core'; import { defineIntegration } from '@sentry/core'; import type { HttpIncomingMessage, HttpServerResponse } from '@sentry/core/server';