diff --git a/packages/core/src/scope.ts b/packages/core/src/scope.ts index 8f05cf78c16f..bd2c11b0fa4d 100644 --- a/packages/core/src/scope.ts +++ b/packages/core/src/scope.ts @@ -755,7 +755,7 @@ export class Scope { * @returns {string} The id of the captured event. */ public captureEvent(event: Event, hint?: EventHint): string { - const eventId = hint?.event_id || uuid4(); + const eventId = event.event_id || hint?.event_id || uuid4(); if (!this._client) { DEBUG_BUILD && debug.warn('No client configured on scope - will not capture event!'); diff --git a/packages/core/test/lib/scope.test.ts b/packages/core/test/lib/scope.test.ts index 11fc4cb62fff..34b04cf45a77 100644 --- a/packages/core/test/lib/scope.test.ts +++ b/packages/core/test/lib/scope.test.ts @@ -10,6 +10,7 @@ import { import { Scope } from '../../src/scope'; import type { Breadcrumb } from '../../src/types-hoist/breadcrumb'; import type { Event } from '../../src/types-hoist/event'; +import { uuid4 } from '../../src/utils/misc'; import { applyScopeDataToEvent } from '../../src/utils/scopeData'; import { getDefaultTestClientOptions, TestClient } from '../mocks/client'; import { clearGlobalScope } from '../testutils'; @@ -1009,6 +1010,54 @@ describe('Scope', () => { scope, ); }); + + it('should return event_id from event object when provided', () => { + const fakeCaptureEvent = vi.fn(() => 'mock-event-id'); + const fakeClient = { + captureEvent: fakeCaptureEvent, + } as unknown as Client; + const scope = new Scope(); + scope.setClient(fakeClient); + + const customEventId = uuid4(); + const eventId = scope.captureEvent({ event_id: customEventId }); + + expect(eventId).toBe(customEventId); + expect(fakeCaptureEvent).toHaveBeenCalledWith( + expect.objectContaining({ event_id: customEventId }), + expect.objectContaining({ event_id: customEventId }), + scope, + ); + }); + + it('should prefer event.event_id over hint.event_id', () => { + const fakeCaptureEvent = vi.fn(() => 'mock-event-id'); + const fakeClient = { + captureEvent: fakeCaptureEvent, + } as unknown as Client; + const scope = new Scope(); + scope.setClient(fakeClient); + + const eventEventId = uuid4(); + const hintEventId = uuid4(); + const eventId = scope.captureEvent({ event_id: eventEventId }, { event_id: hintEventId }); + + expect(eventId).toBe(eventEventId); + expect(fakeCaptureEvent).toHaveBeenCalledWith( + expect.objectContaining({ event_id: eventEventId }), + expect.objectContaining({ event_id: eventEventId }), + scope, + ); + }); + + it('should return event_id from event object when no client is configured', () => { + const scope = new Scope(); + + const customEventId = uuid4(); + const eventId = scope.captureEvent({ event_id: customEventId }); + + expect(eventId).toBe(customEventId); + }); }); describe('setConversationId() / getScopeData()', () => {