Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
Expand Down
49 changes: 49 additions & 0 deletions packages/core/test/lib/scope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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()', () => {
Expand Down