Skip to content

Commit

Permalink
fix core unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Lms24 committed Apr 25, 2024
1 parent 417a1f9 commit 5095e82
Showing 1 changed file with 85 additions and 2 deletions.
87 changes: 85 additions & 2 deletions packages/core/test/lib/envelope.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import type { DsnComponents, DynamicSamplingContext, Event } from '@sentry/types';
import type { Client, DsnComponents, DynamicSamplingContext, Event } from '@sentry/types';

import { createEventEnvelope } from '../../src/envelope';
import {
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
SentrySpan,
getCurrentScope,
getIsolationScope,
setAsyncContextStrategy,
setCurrentClient,
} from '../../src';
import { createEventEnvelope, createSpanEnvelope } from '../../src/envelope';
import { TestClient, getDefaultTestClientOptions } from '../mocks/client';

const testDsn: DsnComponents = { protocol: 'https', projectId: 'abc', host: 'testry.io', publicKey: 'pubKey123' };

Expand Down Expand Up @@ -75,3 +84,77 @@ describe('createEventEnvelope', () => {
});
});
});

describe('createSpanEnvelope', () => {
let client: Client | undefined;
beforeEach(() => {
getCurrentScope().clear();
getIsolationScope().clear();
setAsyncContextStrategy(undefined);
const options = getDefaultTestClientOptions({ tracesSampleRate: 1, dsn: 'https://username@domain/123' });
client = new TestClient(options);
setCurrentClient(client);
client.init();
});

it('creates a span envelope', () => {
const span = new SentrySpan({
name: 'test',
isStandalone: true,
startTimestamp: 1,
endTimestamp: 2,
sampled: true,
attributes: { [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'custom' },
});

const spanEnvelope = createSpanEnvelope([span]);

const spanItem = spanEnvelope[1][0][1];
expect(spanItem).toEqual({
data: {
'sentry.origin': 'manual',
'sentry.source': 'custom',
},
description: 'test',
is_segment: true,
origin: 'manual',
span_id: expect.stringMatching(/^[0-9a-f]{16}$/),
segment_id: spanItem.segment_id,
start_timestamp: 1,
timestamp: 2,
trace_id: expect.stringMatching(/^[0-9a-f]{32}$/),
});
});

it('adds `trace` and `sent_at` envelope headers', () => {
const spanEnvelope = createSpanEnvelope([
new SentrySpan({ name: 'test', attributes: { [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'custom' } }),
]);

const spanEnvelopeHeaders = spanEnvelope[0];
expect(spanEnvelopeHeaders).toEqual({
sent_at: expect.any(String),
trace: {
environment: 'production',
public_key: 'username',
sampled: 'false',
trace_id: expect.stringMatching(/^[0-9a-f]{32}$/),
transaction: 'test',
},
});
});

it("doesn't add a `trace` envelope header if there's no public key", () => {
const options = getDefaultTestClientOptions({ tracesSampleRate: 1, dsn: 'https://domain/123' });
client = new TestClient(options);
setCurrentClient(client);
client.init();

const spanEnvelope = createSpanEnvelope([new SentrySpan()]);

const spanEnvelopeHeaders = spanEnvelope[0];
expect(spanEnvelopeHeaders).toEqual({
sent_at: expect.any(String),
});
});
});

0 comments on commit 5095e82

Please sign in to comment.