Skip to content

Commit 75ee7af

Browse files
committed
fix and clean up tests
1 parent f690691 commit 75ee7af

File tree

2 files changed

+85
-75
lines changed

2 files changed

+85
-75
lines changed

packages/core/test/lib/request.test.ts

Lines changed: 65 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
1-
import { Event, TransactionSamplingMethod } from '@sentry/types';
1+
import { DebugMeta, Event, SentryRequest, TransactionSamplingMethod } from '@sentry/types';
22

33
import { API } from '../../src/api';
44
import { eventToSentryRequest } from '../../src/request';
55

66
describe('eventToSentryRequest', () => {
7-
let api: API;
7+
function parseEnvelopeRequest(request: SentryRequest): any {
8+
const [envelopeHeaderString, itemHeaderString, eventString] = request.body.split('\n');
9+
10+
return {
11+
envelopeHeader: JSON.parse(envelopeHeaderString),
12+
itemHeader: JSON.parse(itemHeaderString),
13+
event: JSON.parse(eventString),
14+
};
15+
}
16+
17+
const api = new API('https://[email protected]/12312012', {
18+
sdk: {
19+
integrations: ['AWSLambda'],
20+
name: 'sentry.javascript.browser',
21+
version: `12.31.12`,
22+
packages: [{ name: 'npm:@sentry/browser', version: `12.31.12` }],
23+
},
24+
});
825
let event: Event;
926

1027
beforeEach(() => {
11-
api = new API('https://[email protected]/12312012', {
12-
sdk: {
13-
integrations: ['AWSLambda'],
14-
name: 'sentry.javascript.browser',
15-
version: `12.31.12`,
16-
packages: [{ name: 'npm:@sentry/browser', version: `6.6.6` }],
17-
},
18-
});
1928
event = {
2029
contexts: { trace: { trace_id: '1231201211212012', span_id: '12261980', op: 'pageload' } },
2130
environment: 'dogpark',
@@ -28,68 +37,63 @@ describe('eventToSentryRequest', () => {
2837
};
2938
});
3039

31-
[
32-
{ method: TransactionSamplingMethod.Rate, rate: '0.1121', dog: 'Charlie' },
33-
{ method: TransactionSamplingMethod.Sampler, rate: '0.1231', dog: 'Maisey' },
34-
{ method: TransactionSamplingMethod.Inheritance, dog: 'Cory' },
35-
{ method: TransactionSamplingMethod.Explicit, dog: 'Bodhi' },
36-
37-
// this shouldn't ever happen (at least the method should always be included in tags), but good to know that things
38-
// won't blow up if it does
39-
{ dog: 'Lucy' },
40-
].forEach(({ method, rate, dog }) => {
41-
it(`adds transaction sampling information to item header - ${method}, ${rate}, ${dog}`, () => {
42-
// TODO kmclb - once tag types are loosened, don't need to cast to string here
43-
event.tags = { __sentry_samplingMethod: String(method), __sentry_sampleRate: String(rate), dog };
44-
45-
const result = eventToSentryRequest(event, api);
46-
47-
const [envelopeHeaderString, itemHeaderString, eventString] = result.body.split('\n');
48-
49-
const envelope = {
50-
envelopeHeader: JSON.parse(envelopeHeaderString),
51-
itemHeader: JSON.parse(itemHeaderString),
52-
event: JSON.parse(eventString),
53-
};
54-
55-
// the right stuff is added to the item header
56-
expect(envelope.itemHeader).toEqual({
57-
type: 'transaction',
58-
// TODO kmclb - once tag types are loosened, don't need to cast to string here
59-
sample_rates: [{ id: String(method), rate: String(rate) }],
60-
});
61-
62-
// show that it pops the right tags and leaves the rest alone
63-
expect('__sentry_samplingMethod' in envelope.event.tags).toBe(false);
64-
expect('__sentry_sampleRate' in envelope.event.tags).toBe(false);
65-
expect('dog' in envelope.event.tags).toBe(true);
66-
});
40+
it(`adds transaction sampling information to item header`, () => {
41+
event.debug_meta = { transactionSampling: { method: TransactionSamplingMethod.Rate, rate: 0.1121 } };
42+
43+
const result = eventToSentryRequest(event, api);
44+
const envelope = parseEnvelopeRequest(result);
45+
46+
expect(envelope.itemHeader).toEqual(
47+
expect.objectContaining({
48+
sample_rates: [{ id: TransactionSamplingMethod.Rate, rate: 0.1121 }],
49+
}),
50+
);
6751
});
6852

69-
it('adds sdk info to envelope header', () => {
53+
it('removes transaction sampling information (and only that) from debug_meta', () => {
54+
event.debug_meta = {
55+
transactionSampling: { method: TransactionSamplingMethod.Sampler, rate: 0.1121 },
56+
dog: 'Charlie',
57+
} as DebugMeta;
58+
7059
const result = eventToSentryRequest(event, api);
60+
const envelope = parseEnvelopeRequest(result);
7161

72-
const envelopeHeaderString = result.body.split('\n')[0];
73-
const parsedHeader = JSON.parse(envelopeHeaderString);
62+
expect('transactionSampling' in envelope.event.debug_meta).toBe(false);
63+
expect('dog' in envelope.event.debug_meta).toBe(true);
64+
});
7465

75-
expect(parsedHeader).toEqual(
66+
it('removes debug_meta entirely if it ends up empty', () => {
67+
event.debug_meta = {
68+
transactionSampling: { method: TransactionSamplingMethod.Rate, rate: 0.1121 },
69+
} as DebugMeta;
70+
71+
const result = eventToSentryRequest(event, api);
72+
const envelope = parseEnvelopeRequest(result);
73+
74+
expect('debug_meta' in envelope.event).toBe(false);
75+
});
76+
77+
it('adds sdk info to envelope header', () => {
78+
const result = eventToSentryRequest(event, api);
79+
const envelope = parseEnvelopeRequest(result);
80+
81+
expect(envelope.envelopeHeader).toEqual(
7682
expect.objectContaining({ sdk: { name: 'sentry.javascript.browser', version: '12.31.12' } }),
7783
);
7884
});
7985

8086
it('adds sdk info to event body', () => {
8187
const result = eventToSentryRequest(event, api);
88+
const envelope = parseEnvelopeRequest(result);
8289

83-
const eventString = result.body.split('\n')[2];
84-
const parsedEvent = JSON.parse(eventString);
85-
86-
expect(parsedEvent).toEqual(
90+
expect(envelope.event).toEqual(
8791
expect.objectContaining({
8892
sdk: {
8993
integrations: ['AWSLambda'],
9094
name: 'sentry.javascript.browser',
9195
version: `12.31.12`,
92-
packages: [{ name: 'npm:@sentry/browser', version: `6.6.6` }],
96+
packages: [{ name: 'npm:@sentry/browser', version: `12.31.12` }],
9397
},
9498
}),
9599
);
@@ -99,22 +103,21 @@ describe('eventToSentryRequest', () => {
99103
event.sdk = {
100104
integrations: ['Clojure'],
101105
name: 'foo',
102-
packages: [{ name: 'npm:@sentry/clj', version: `6.6.6` }],
106+
packages: [{ name: 'npm:@sentry/clj', version: `12.31.12` }],
103107
version: '1337',
104108
};
105-
const result = eventToSentryRequest(event, api);
106109

107-
const eventString = result.body.split('\n')[2];
108-
const parsedEvent = JSON.parse(eventString);
110+
const result = eventToSentryRequest(event, api);
111+
const envelope = parseEnvelopeRequest(result);
109112

110-
expect(parsedEvent).toEqual(
113+
expect(envelope.event).toEqual(
111114
expect.objectContaining({
112115
sdk: {
113116
integrations: ['Clojure', 'AWSLambda'],
114117
name: 'foo',
115118
packages: [
116-
{ name: 'npm:@sentry/clj', version: `6.6.6` },
117-
{ name: 'npm:@sentry/browser', version: `6.6.6` },
119+
{ name: 'npm:@sentry/clj', version: `12.31.12` },
120+
{ name: 'npm:@sentry/browser', version: `12.31.12` },
118121
],
119122
version: '1337',
120123
},

packages/tracing/test/hub.test.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@
22
import { BrowserClient } from '@sentry/browser';
33
import { getMainCarrier, Hub } from '@sentry/hub';
44
import * as hubModule from '@sentry/hub';
5+
import { TransactionSamplingMethod } from '@sentry/types';
56
import * as utilsModule from '@sentry/utils'; // for mocking
67
import { getGlobalObject, isNodeEnv, logger } from '@sentry/utils';
78
import * as nodeHttpModule from 'http';
89

910
import { BrowserTracing } from '../src/browser/browsertracing';
1011
import { addExtensionMethods } from '../src/hubextensions';
12+
import { Transaction } from '../src/transaction';
1113
import { extractTraceparentData, TRACEPARENT_REGEXP } from '../src/utils';
1214
import { addDOMPropertiesToGlobal, getSymbolObjectKeyByName } from './testutils';
1315

1416
addExtensionMethods();
1517

1618
const mathRandom = jest.spyOn(Math, 'random');
19+
jest.spyOn(Transaction.prototype, 'setMetadata');
1720

1821
// we have to add things into the real global object (rather than mocking the return value of getGlobalObject) because
1922
// there are modules which call getGlobalObject as they load, which is seemingly too early for jest to intervene
@@ -27,7 +30,7 @@ describe('Hub', () => {
2730
});
2831

2932
afterEach(() => {
30-
jest.restoreAllMocks();
33+
jest.clearAllMocks();
3134
jest.useRealTimers();
3235
});
3336

@@ -259,35 +262,39 @@ describe('Hub', () => {
259262
it('should record sampling method when sampling decision is explicitly set', () => {
260263
const tracesSampler = jest.fn().mockReturnValue(0.1121);
261264
const hub = new Hub(new BrowserClient({ tracesSampler }));
262-
const transaction = hub.startTransaction({ name: 'dogpark', sampled: true });
265+
hub.startTransaction({ name: 'dogpark', sampled: true });
263266

264-
expect(transaction.tags).toEqual(expect.objectContaining({ __sentry_samplingMethod: 'explicitly_set' }));
267+
expect(Transaction.prototype.setMetadata).toHaveBeenCalledWith({
268+
transactionSampling: { method: TransactionSamplingMethod.Explicit },
269+
});
265270
});
266271

267272
it('should record sampling method and rate when sampling decision comes from tracesSampler', () => {
268273
const tracesSampler = jest.fn().mockReturnValue(0.1121);
269274
const hub = new Hub(new BrowserClient({ tracesSampler }));
270-
const transaction = hub.startTransaction({ name: 'dogpark' });
275+
hub.startTransaction({ name: 'dogpark' });
271276

272-
expect(transaction.tags).toEqual(
273-
expect.objectContaining({ __sentry_samplingMethod: 'client_sampler', __sentry_sampleRate: '0.1121' }),
274-
);
277+
expect(Transaction.prototype.setMetadata).toHaveBeenCalledWith({
278+
transactionSampling: { method: TransactionSamplingMethod.Sampler, rate: 0.1121 },
279+
});
275280
});
276281

277282
it('should record sampling method when sampling decision is inherited', () => {
278283
const hub = new Hub(new BrowserClient({ tracesSampleRate: 0.1121 }));
279-
const transaction = hub.startTransaction({ name: 'dogpark', parentSampled: true });
284+
hub.startTransaction({ name: 'dogpark', parentSampled: true });
280285

281-
expect(transaction.tags).toEqual(expect.objectContaining({ __sentry_samplingMethod: 'inheritance' }));
286+
expect(Transaction.prototype.setMetadata).toHaveBeenCalledWith({
287+
transactionSampling: { method: TransactionSamplingMethod.Inheritance },
288+
});
282289
});
283290

284291
it('should record sampling method and rate when sampling decision comes from traceSampleRate', () => {
285292
const hub = new Hub(new BrowserClient({ tracesSampleRate: 0.1121 }));
286-
const transaction = hub.startTransaction({ name: 'dogpark' });
293+
hub.startTransaction({ name: 'dogpark' });
287294

288-
expect(transaction.tags).toEqual(
289-
expect.objectContaining({ __sentry_samplingMethod: 'client_rate', __sentry_sampleRate: '0.1121' }),
290-
);
295+
expect(Transaction.prototype.setMetadata).toHaveBeenCalledWith({
296+
transactionSampling: { method: TransactionSamplingMethod.Rate, rate: 0.1121 },
297+
});
291298
});
292299
});
293300

0 commit comments

Comments
 (0)