|
| 1 | +import * as SentryCore from '@sentry/core'; |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 3 | +import { _sendStandaloneLcpSpan, isValidLcpMetric, MAX_PLAUSIBLE_LCP_DURATION } from '../../src/metrics/lcp'; |
| 4 | +import * as WebVitalUtils from '../../src/metrics/utils'; |
| 5 | + |
| 6 | +vi.mock('@sentry/core', async () => { |
| 7 | + const actual = await vi.importActual('@sentry/core'); |
| 8 | + return { |
| 9 | + ...actual, |
| 10 | + browserPerformanceTimeOrigin: vi.fn(), |
| 11 | + getCurrentScope: vi.fn(), |
| 12 | + htmlTreeAsString: vi.fn(), |
| 13 | + }; |
| 14 | +}); |
| 15 | + |
| 16 | +describe('isValidLcpMetric', () => { |
| 17 | + it('returns true for plausible lcp values', () => { |
| 18 | + expect(isValidLcpMetric(0)).toBe(true); |
| 19 | + expect(isValidLcpMetric(2_500)).toBe(true); |
| 20 | + expect(isValidLcpMetric(MAX_PLAUSIBLE_LCP_DURATION)).toBe(true); |
| 21 | + }); |
| 22 | + |
| 23 | + it('returns false for implausible lcp values', () => { |
| 24 | + expect(isValidLcpMetric(undefined)).toBe(false); |
| 25 | + expect(isValidLcpMetric(-1)).toBe(false); |
| 26 | + expect(isValidLcpMetric(MAX_PLAUSIBLE_LCP_DURATION + 1)).toBe(false); |
| 27 | + }); |
| 28 | +}); |
| 29 | + |
| 30 | +describe('_sendStandaloneLcpSpan', () => { |
| 31 | + const mockSpan = { |
| 32 | + addEvent: vi.fn(), |
| 33 | + end: vi.fn(), |
| 34 | + }; |
| 35 | + |
| 36 | + const mockScope = { |
| 37 | + getScopeData: vi.fn().mockReturnValue({ |
| 38 | + transactionName: 'test-transaction', |
| 39 | + }), |
| 40 | + }; |
| 41 | + |
| 42 | + beforeEach(() => { |
| 43 | + vi.mocked(SentryCore.getCurrentScope).mockReturnValue(mockScope as any); |
| 44 | + vi.mocked(SentryCore.browserPerformanceTimeOrigin).mockReturnValue(1000); |
| 45 | + vi.mocked(SentryCore.htmlTreeAsString).mockImplementation((node: any) => `<${node?.tagName || 'div'}>`); |
| 46 | + vi.spyOn(WebVitalUtils, 'startStandaloneWebVitalSpan').mockReturnValue(mockSpan as any); |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(() => { |
| 50 | + vi.clearAllMocks(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('sends a standalone lcp span with entry data', () => { |
| 54 | + const lcpValue = 1_234; |
| 55 | + const mockEntry: LargestContentfulPaint = { |
| 56 | + name: 'largest-contentful-paint', |
| 57 | + entryType: 'largest-contentful-paint', |
| 58 | + startTime: 100, |
| 59 | + duration: 0, |
| 60 | + id: 'image', |
| 61 | + url: 'https://example.com/image.png', |
| 62 | + size: 1234, |
| 63 | + loadTime: 95, |
| 64 | + renderTime: 100, |
| 65 | + element: { tagName: 'img' } as Element, |
| 66 | + toJSON: vi.fn(), |
| 67 | + }; |
| 68 | + |
| 69 | + _sendStandaloneLcpSpan(lcpValue, mockEntry, '123', 'navigation'); |
| 70 | + |
| 71 | + expect(WebVitalUtils.startStandaloneWebVitalSpan).toHaveBeenCalledWith({ |
| 72 | + name: '<img>', |
| 73 | + transaction: 'test-transaction', |
| 74 | + attributes: { |
| 75 | + 'sentry.origin': 'auto.http.browser.lcp', |
| 76 | + 'sentry.op': 'ui.webvital.lcp', |
| 77 | + 'sentry.exclusive_time': 0, |
| 78 | + 'sentry.pageload.span_id': '123', |
| 79 | + 'sentry.report_event': 'navigation', |
| 80 | + 'lcp.element': '<img>', |
| 81 | + 'lcp.id': 'image', |
| 82 | + 'lcp.url': 'https://example.com/image.png', |
| 83 | + 'lcp.loadTime': 95, |
| 84 | + 'lcp.renderTime': 100, |
| 85 | + 'lcp.size': 1234, |
| 86 | + }, |
| 87 | + startTime: 1.1, |
| 88 | + }); |
| 89 | + |
| 90 | + expect(mockSpan.addEvent).toHaveBeenCalledWith('lcp', { |
| 91 | + 'sentry.measurement_unit': 'millisecond', |
| 92 | + 'sentry.measurement_value': lcpValue, |
| 93 | + }); |
| 94 | + expect(mockSpan.end).toHaveBeenCalledWith(1.1); |
| 95 | + }); |
| 96 | + |
| 97 | + it('does not send a standalone lcp span for implausibly large values', () => { |
| 98 | + _sendStandaloneLcpSpan(MAX_PLAUSIBLE_LCP_DURATION + 1, undefined, '123', 'pagehide'); |
| 99 | + |
| 100 | + expect(WebVitalUtils.startStandaloneWebVitalSpan).not.toHaveBeenCalled(); |
| 101 | + expect(mockSpan.addEvent).not.toHaveBeenCalled(); |
| 102 | + expect(mockSpan.end).not.toHaveBeenCalled(); |
| 103 | + }); |
| 104 | +}); |
0 commit comments