Skip to content

Commit cbe2971

Browse files
committed
add suggested tests
1 parent d53dea3 commit cbe2971

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

packages/tracing/test/hub.test.ts

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { addExtensionMethods } from '../src/hubextensions';
99

1010
addExtensionMethods();
1111

12+
const mathRandom = jest.spyOn(Math, 'random');
1213
describe('Hub', () => {
1314
beforeEach(() => {
1415
jest.spyOn(logger, 'warn');
@@ -178,6 +179,24 @@ describe('Hub', () => {
178179
expect(tracesSampler).toHaveBeenCalled();
179180
});
180181

182+
it('should set sampled = false if tracesSampler returns 0', () => {
183+
const tracesSampler = jest.fn().mockReturnValue(0);
184+
const hub = new Hub(new BrowserClient({ tracesSampler }));
185+
const transaction = hub.startTransaction({ name: 'dogpark' });
186+
187+
expect(tracesSampler).toHaveBeenCalled();
188+
expect(transaction.sampled).toBe(false);
189+
});
190+
191+
it('should set sampled = true if tracesSampler returns 1', () => {
192+
const tracesSampler = jest.fn().mockReturnValue(1);
193+
const hub = new Hub(new BrowserClient({ tracesSampler }));
194+
const transaction = hub.startTransaction({ name: 'dogpark' });
195+
196+
expect(tracesSampler).toHaveBeenCalled();
197+
expect(transaction.sampled).toBe(true);
198+
});
199+
181200
it('should prefer tracesSampler to tracesSampleRate', () => {
182201
// make the two options do opposite things to prove precedence
183202
const tracesSampler = jest.fn().mockReturnValue(true);
@@ -296,7 +315,24 @@ describe('Hub', () => {
296315
// TODO fix this and write the test
297316
});
298317

299-
it("should inherit parent's sampling decision when creating a new transaction if tracesSampler is undefined", () => {
318+
it("should inherit parent's positive sampling decision if tracesSampler is undefined", () => {
319+
// we know that without inheritance we'll get sampled = false (since our "random" number won't be below the
320+
// sample rate), so make parent's decision the opposite to prove that inheritance takes precedence over
321+
// tracesSampleRate
322+
mathRandom.mockReturnValueOnce(1);
323+
const hub = new Hub(new BrowserClient({ tracesSampleRate: 0.5 }));
324+
const parentSamplingDecsion = true;
325+
326+
const transaction = hub.startTransaction({
327+
name: 'dogpark',
328+
parentSpanId: '12312012',
329+
parentSampled: parentSamplingDecsion,
330+
});
331+
332+
expect(transaction.sampled).toBe(parentSamplingDecsion);
333+
});
334+
335+
it("should inherit parent's negative sampling decision if tracesSampler is undefined", () => {
300336
// tracesSampleRate = 1 means every transaction should end up with sampled = true, so make parent's decision the
301337
// opposite to prove that inheritance takes precedence over tracesSampleRate
302338
const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }));
@@ -311,7 +347,7 @@ describe('Hub', () => {
311347
expect(transaction.sampled).toBe(parentSamplingDecsion);
312348
});
313349

314-
it("should ignore parent's sampling decision when tracesSampler is defined", () => {
350+
it("should ignore parent's positive sampling decision when tracesSampler is defined", () => {
315351
// this tracesSampler causes every transaction to end up with sampled = true, so make parent's decision the
316352
// opposite to prove that tracesSampler takes precedence over inheritance
317353
const tracesSampler = () => true;
@@ -327,6 +363,23 @@ describe('Hub', () => {
327363

328364
expect(transaction.sampled).not.toBe(parentSamplingDecsion);
329365
});
366+
367+
it("should ignore parent's negative sampling decision when tracesSampler is defined", () => {
368+
// this tracesSampler causes every transaction to end up with sampled = false, so make parent's decision the
369+
// opposite to prove that tracesSampler takes precedence over inheritance
370+
const tracesSampler = () => false;
371+
const parentSamplingDecsion = true;
372+
373+
const hub = new Hub(new BrowserClient({ tracesSampler }));
374+
375+
const transaction = hub.startTransaction({
376+
name: 'dogpark',
377+
parentSpanId: '12312012',
378+
parentSampled: parentSamplingDecsion,
379+
});
380+
381+
expect(transaction.sampled).not.toBe(parentSamplingDecsion);
382+
});
330383
});
331384
});
332385
});

0 commit comments

Comments
 (0)