|
| 1 | +import { getActiveSpan, getClient, startInactiveSpan, startSpan, withActiveSpan } from '../../src'; |
| 2 | +import { cleanupOtel, mockSdkInit } from '../helpers/mockSdkInit'; |
| 3 | + |
| 4 | +afterEach(() => { |
| 5 | + jest.restoreAllMocks(); |
| 6 | + cleanupOtel(); |
| 7 | +}); |
| 8 | + |
| 9 | +describe('withActiveSpan()', () => { |
| 10 | + it('should set the active span within the callback', () => { |
| 11 | + mockSdkInit(); |
| 12 | + |
| 13 | + const inactiveSpan = startInactiveSpan({ name: 'inactive-span' }); |
| 14 | + |
| 15 | + expect(getActiveSpan()).not.toBe(inactiveSpan); |
| 16 | + |
| 17 | + withActiveSpan(inactiveSpan, () => { |
| 18 | + expect(getActiveSpan()).toBe(inactiveSpan); |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should create child spans when calling startSpan within the callback', async () => { |
| 23 | + const beforeSendTransaction = jest.fn(() => null); |
| 24 | + mockSdkInit({ enableTracing: true, beforeSendTransaction }); |
| 25 | + const client = getClient(); |
| 26 | + |
| 27 | + const inactiveSpan = startInactiveSpan({ name: 'inactive-span' }); |
| 28 | + |
| 29 | + withActiveSpan(inactiveSpan, () => { |
| 30 | + startSpan({ name: 'child-span' }, () => {}); |
| 31 | + }); |
| 32 | + |
| 33 | + startSpan({ name: 'floating-span' }, () => {}); |
| 34 | + |
| 35 | + inactiveSpan.end(); |
| 36 | + |
| 37 | + await client.flush(); |
| 38 | + |
| 39 | + // The child span should be a child of the inactive span |
| 40 | + expect(beforeSendTransaction).toHaveBeenCalledWith( |
| 41 | + expect.objectContaining({ |
| 42 | + transaction: 'inactive-span', |
| 43 | + spans: expect.arrayContaining([expect.objectContaining({ description: 'child-span' })]), |
| 44 | + }), |
| 45 | + expect.anything(), |
| 46 | + ); |
| 47 | + |
| 48 | + // The floating span should be a separate transaction |
| 49 | + expect(beforeSendTransaction).toHaveBeenCalledWith( |
| 50 | + expect.objectContaining({ |
| 51 | + transaction: 'floating-span', |
| 52 | + }), |
| 53 | + expect.anything(), |
| 54 | + ); |
| 55 | + }); |
| 56 | +}); |
0 commit comments