|
1 |
| -import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 1 | +import { getTraceMetaTags } from '@sentry/core'; |
| 2 | +import { type Mock, afterEach, describe, expect, it, vi } from 'vitest'; |
2 | 3 | import { addSentryTracingMetaTags } from '../../../src/runtime/utils';
|
3 | 4 |
|
4 |
| -const mockReturns = vi.hoisted(() => { |
5 |
| - return { |
6 |
| - traceHeader: 'trace-header', |
7 |
| - baggageHeader: 'baggage-header', |
8 |
| - }; |
9 |
| -}); |
10 |
| - |
11 |
| -vi.mock('@sentry/core', async () => { |
12 |
| - const actual = await vi.importActual('@sentry/core'); |
13 |
| - |
14 |
| - return { |
15 |
| - ...actual, |
16 |
| - getActiveSpan: vi.fn().mockReturnValue({ spanId: '123' }), |
17 |
| - getRootSpan: vi.fn().mockReturnValue({ spanId: 'root123' }), |
18 |
| - spanToTraceHeader: vi.fn(() => mockReturns.traceHeader), |
19 |
| - }; |
20 |
| -}); |
21 |
| - |
22 |
| -vi.mock('@sentry/opentelemetry', async () => { |
23 |
| - const actual = await vi.importActual('@sentry/opentelemetry'); |
24 |
| - |
25 |
| - return { |
26 |
| - ...actual, |
27 |
| - getDynamicSamplingContextFromSpan: vi.fn().mockReturnValue('contextValue'), |
28 |
| - }; |
29 |
| -}); |
30 |
| - |
31 |
| -vi.mock('@sentry/utils', async () => { |
32 |
| - const actual = await vi.importActual('@sentry/utils'); |
33 |
| - |
34 |
| - return { |
35 |
| - ...actual, |
36 |
| - dynamicSamplingContextToSentryBaggageHeader: vi.fn().mockReturnValue(mockReturns.baggageHeader), |
37 |
| - }; |
38 |
| -}); |
| 5 | +vi.mock('@sentry/core', () => ({ |
| 6 | + getTraceMetaTags: vi.fn(), |
| 7 | +})); |
39 | 8 |
|
40 | 9 | describe('addSentryTracingMetaTags', () => {
|
41 | 10 | afterEach(() => {
|
42 | 11 | vi.resetAllMocks();
|
43 | 12 | });
|
44 | 13 |
|
45 |
| - it('should add meta tags when there is an active root span', () => { |
| 14 | + it('should add meta tags to the head array', () => { |
| 15 | + const mockMetaTags = [ |
| 16 | + '<meta name="sentry-trace" content="12345678901234567890123456789012-1234567890123456-1"/>', |
| 17 | + '<meta name="baggage" content="sentry-environment=production"/>', |
| 18 | + ].join('\n'); |
| 19 | + |
| 20 | + // return value is mocked here as return values of `getTraceMetaTags` are tested separately (in @sentry/core) |
| 21 | + (getTraceMetaTags as Mock).mockReturnValue(mockMetaTags); |
| 22 | + |
46 | 23 | const head: string[] = [];
|
47 | 24 | addSentryTracingMetaTags(head);
|
48 | 25 |
|
49 |
| - expect(head).toContain(`<meta name="sentry-trace" content="${mockReturns.traceHeader}"/>`); |
50 |
| - expect(head).toContain(`<meta name="baggage" content="${mockReturns.baggageHeader}"/>`); |
| 26 | + expect(head).toContain(mockMetaTags); |
51 | 27 | });
|
52 | 28 |
|
53 |
| - it('should not add meta tags when there is no active root span', () => { |
54 |
| - vi.doMock('@sentry/core', async () => { |
55 |
| - const actual = await vi.importActual('@sentry/core'); |
56 |
| - |
57 |
| - return { |
58 |
| - ...actual, |
59 |
| - getActiveSpan: vi.fn().mockReturnValue(undefined), |
60 |
| - }; |
61 |
| - }); |
| 29 | + it('should handle empty meta tags', () => { |
| 30 | + (getTraceMetaTags as Mock).mockReturnValue(''); |
62 | 31 |
|
63 | 32 | const head: string[] = [];
|
64 | 33 | addSentryTracingMetaTags(head);
|
|
0 commit comments