|
| 1 | +import { expect } from '@playwright/test'; |
| 2 | +import type { Event, Profile } from '@sentry/core'; |
| 3 | +import { sentryTest } from '../../../utils/fixtures'; |
| 4 | +import { |
| 5 | + properEnvelopeRequestParser, |
| 6 | + shouldSkipTracingTest, |
| 7 | + waitForTransactionRequestOnUrl, |
| 8 | +} from '../../../utils/helpers'; |
| 9 | + |
| 10 | +sentryTest( |
| 11 | + 'does not send profile envelope when document-policy is not set', |
| 12 | + async ({ page, getLocalTestUrl, browserName }) => { |
| 13 | + if (shouldSkipTracingTest() || browserName !== 'chromium') { |
| 14 | + // Profiling only works when tracing is enabled |
| 15 | + sentryTest.skip(); |
| 16 | + } |
| 17 | + |
| 18 | + const url = await getLocalTestUrl({ testDir: __dirname }); |
| 19 | + |
| 20 | + const req = await waitForTransactionRequestOnUrl(page, url); |
| 21 | + const transactionEvent = properEnvelopeRequestParser<Event>(req, 0); |
| 22 | + const profileEvent = properEnvelopeRequestParser<Profile>(req, 1); |
| 23 | + |
| 24 | + expect(transactionEvent).toBeDefined(); |
| 25 | + expect(profileEvent).toBeUndefined(); |
| 26 | + }, |
| 27 | +); |
| 28 | + |
| 29 | +sentryTest('sends profile envelope in legacy mode', async ({ page, getLocalTestUrl, browserName }) => { |
| 30 | + if (shouldSkipTracingTest() || browserName !== 'chromium') { |
| 31 | + // Profiling only works when tracing is enabled |
| 32 | + sentryTest.skip(); |
| 33 | + } |
| 34 | + |
| 35 | + const url = await getLocalTestUrl({ testDir: __dirname, responseHeaders: { 'Document-Policy': 'js-profiling' } }); |
| 36 | + |
| 37 | + const req = await waitForTransactionRequestOnUrl(page, url); |
| 38 | + const profileEvent = properEnvelopeRequestParser<Profile>(req, 1); |
| 39 | + expect(profileEvent).toBeDefined(); |
| 40 | + |
| 41 | + const profile = profileEvent.profile; |
| 42 | + expect(profileEvent.profile).toBeDefined(); |
| 43 | + |
| 44 | + expect(profile.samples).toBeDefined(); |
| 45 | + expect(profile.stacks).toBeDefined(); |
| 46 | + expect(profile.frames).toBeDefined(); |
| 47 | + expect(profile.thread_metadata).toBeDefined(); |
| 48 | + |
| 49 | + // Samples |
| 50 | + expect(profile.samples.length).toBeGreaterThanOrEqual(2); |
| 51 | + for (const sample of profile.samples) { |
| 52 | + expect(typeof sample.elapsed_since_start_ns).toBe('string'); |
| 53 | + expect(sample.elapsed_since_start_ns).toMatch(/^\d+$/); // Numeric string |
| 54 | + expect(parseInt(sample.elapsed_since_start_ns, 10)).toBeGreaterThanOrEqual(0); |
| 55 | + |
| 56 | + expect(typeof sample.stack_id).toBe('number'); |
| 57 | + expect(sample.stack_id).toBeGreaterThanOrEqual(0); |
| 58 | + expect(sample.thread_id).toBe('0'); // Should be main thread |
| 59 | + } |
| 60 | + |
| 61 | + // Stacks |
| 62 | + expect(profile.stacks.length).toBeGreaterThan(0); |
| 63 | + for (const stack of profile.stacks) { |
| 64 | + expect(Array.isArray(stack)).toBe(true); |
| 65 | + for (const frameIndex of stack) { |
| 66 | + expect(typeof frameIndex).toBe('number'); |
| 67 | + expect(frameIndex).toBeGreaterThanOrEqual(0); |
| 68 | + expect(frameIndex).toBeLessThan(profile.frames.length); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Frames |
| 73 | + expect(profile.frames.length).toBeGreaterThan(0); |
| 74 | + for (const frame of profile.frames) { |
| 75 | + expect(frame).toHaveProperty('function'); |
| 76 | + expect(frame).toHaveProperty('abs_path'); |
| 77 | + expect(frame).toHaveProperty('lineno'); |
| 78 | + expect(frame).toHaveProperty('colno'); |
| 79 | + |
| 80 | + expect(typeof frame.function).toBe('string'); |
| 81 | + expect(typeof frame.abs_path).toBe('string'); |
| 82 | + expect(typeof frame.lineno).toBe('number'); |
| 83 | + expect(typeof frame.colno).toBe('number'); |
| 84 | + } |
| 85 | + |
| 86 | + const functionNames = profile.frames.map(frame => frame.function).filter(name => name !== ''); |
| 87 | + |
| 88 | + if ((process.env.PW_BUNDLE || '').endsWith('min')) { |
| 89 | + // Function names are minified in minified bundles |
| 90 | + expect(functionNames.length).toBeGreaterThan(0); |
| 91 | + expect((functionNames as string[]).every(name => name?.length > 0)).toBe(true); // Just make sure they're not empty strings |
| 92 | + } else { |
| 93 | + expect(functionNames).toEqual( |
| 94 | + expect.arrayContaining([ |
| 95 | + '_startRootSpan', |
| 96 | + 'withScope', |
| 97 | + 'createChildOrRootSpan', |
| 98 | + 'startSpanManual', |
| 99 | + 'startProfileForSpan', |
| 100 | + 'startJSSelfProfile', |
| 101 | + ]), |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + expect(profile.thread_metadata).toHaveProperty('0'); |
| 106 | + expect(profile.thread_metadata['0']).toHaveProperty('name'); |
| 107 | + expect(profile.thread_metadata['0'].name).toBe('main'); |
| 108 | + |
| 109 | + // Test that profile duration makes sense (should be > 20ms based on test setup) |
| 110 | + const startTime = parseInt(profile.samples[0].elapsed_since_start_ns, 10); |
| 111 | + const endTime = parseInt(profile.samples[profile.samples.length - 1].elapsed_since_start_ns, 10); |
| 112 | + const durationNs = endTime - startTime; |
| 113 | + const durationMs = durationNs / 1_000_000; // Convert ns to ms |
| 114 | + |
| 115 | + // Should be at least 20ms based on our setTimeout(21) in the test |
| 116 | + expect(durationMs).toBeGreaterThan(20); |
| 117 | +}); |
0 commit comments