|
1 | 1 | import type { Attachment, Breadcrumb, Client, Event } from '@sentry/types';
|
2 |
| -import { applyScopeDataToEvent, getCurrentScope, getIsolationScope, withIsolationScope } from '../../src'; |
| 2 | +import { |
| 3 | + Hub, |
| 4 | + addTracingExtensions, |
| 5 | + applyScopeDataToEvent, |
| 6 | + getActiveSpan, |
| 7 | + getCurrentScope, |
| 8 | + getIsolationScope, |
| 9 | + makeMain, |
| 10 | + startInactiveSpan, |
| 11 | + startSpan, |
| 12 | + withIsolationScope, |
| 13 | +} from '../../src'; |
| 14 | + |
| 15 | +import { withActiveSpan } from '../../src/exports'; |
3 | 16 | import { Scope, getGlobalScope, setGlobalScope } from '../../src/scope';
|
| 17 | +import { TestClient, getDefaultTestClientOptions } from '../mocks/client'; |
4 | 18 |
|
5 | 19 | describe('Scope', () => {
|
6 | 20 | beforeEach(() => {
|
@@ -503,3 +517,40 @@ describe('isolation scope', () => {
|
503 | 517 | });
|
504 | 518 | });
|
505 | 519 | });
|
| 520 | + |
| 521 | +describe('withActiveSpan()', () => { |
| 522 | + beforeAll(() => { |
| 523 | + addTracingExtensions(); |
| 524 | + }); |
| 525 | + |
| 526 | + beforeEach(() => { |
| 527 | + const options = getDefaultTestClientOptions({ enableTracing: true }); |
| 528 | + const client = new TestClient(options); |
| 529 | + const scope = new Scope(); |
| 530 | + const hub = new Hub(client, scope); |
| 531 | + makeMain(hub); |
| 532 | + }); |
| 533 | + |
| 534 | + it('should set the active span within the callback', () => { |
| 535 | + expect.assertions(2); |
| 536 | + const inactiveSpan = startInactiveSpan({ name: 'inactive-span' }); |
| 537 | + |
| 538 | + expect(getActiveSpan()).not.toBe(inactiveSpan); |
| 539 | + |
| 540 | + withActiveSpan(inactiveSpan!, () => { |
| 541 | + expect(getActiveSpan()).toBe(inactiveSpan); |
| 542 | + }); |
| 543 | + }); |
| 544 | + |
| 545 | + it('should create child spans when calling startSpan within the callback', done => { |
| 546 | + expect.assertions(1); |
| 547 | + const inactiveSpan = startInactiveSpan({ name: 'inactive-span' }); |
| 548 | + |
| 549 | + withActiveSpan(inactiveSpan!, () => { |
| 550 | + startSpan({ name: 'child-span' }, childSpan => { |
| 551 | + expect(childSpan?.parentSpanId).toBe(inactiveSpan?.spanContext().spanId); |
| 552 | + done(); |
| 553 | + }); |
| 554 | + }); |
| 555 | + }); |
| 556 | +}); |
0 commit comments