|
| 1 | +import type { Route } from '@playwright/test'; |
| 2 | +import { expect } from '@playwright/test'; |
| 3 | +import type { Event } from '@sentry/types'; |
| 4 | + |
| 5 | +import { sentryTest } from '../../../../utils/fixtures'; |
| 6 | +import { getFirstSentryEnvelopeRequest, shouldSkipTracingTest } from '../../../../utils/helpers'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Bit of an odd test but we previously ran into cases where we would report TTFB > (LCP, FCP, FP) |
| 10 | + * This should never happen and this test serves as a regression test for that. |
| 11 | + * |
| 12 | + * The problem is: We don't always get valid TTFB from the web-vitals library, so we skip the test if that's the case. |
| 13 | + * Note: There is another test that covers that we actually report TTFB if it is valid (@see ../web-vitals-lcp/test.ts). |
| 14 | + */ |
| 15 | +sentryTest('paint web vitals values are greater than TTFB', async ({ browserName, getLocalTestPath, page }) => { |
| 16 | + // Only run in chromium to ensure all vitals are present |
| 17 | + if (shouldSkipTracingTest() || browserName !== 'chromium') { |
| 18 | + sentryTest.skip(); |
| 19 | + } |
| 20 | + |
| 21 | + page.route('**', route => route.continue()); |
| 22 | + page.route('**/library/image.png', async (route: Route) => { |
| 23 | + return route.fulfill({ path: `${__dirname}/assets/sentry-logo-600x179.png` }); |
| 24 | + }); |
| 25 | + |
| 26 | + const url = await getLocalTestPath({ testDir: __dirname }); |
| 27 | + const [eventData] = await Promise.all([ |
| 28 | + getFirstSentryEnvelopeRequest<Event>(page), |
| 29 | + page.goto(url), |
| 30 | + page.locator('button').click(), |
| 31 | + ]); |
| 32 | + |
| 33 | + expect(eventData.measurements).toBeDefined(); |
| 34 | + |
| 35 | + const ttfbValue = eventData.measurements?.ttfb?.value; |
| 36 | + |
| 37 | + if (!ttfbValue) { |
| 38 | + // TTFB is unfortunately quite flaky. Sometimes, the web-vitals library doesn't report TTFB because |
| 39 | + // responseStart is 0. This seems to happen somewhat randomly, so we just ignore this in that case. |
| 40 | + // @see packages/browser-utils/src/metrics/web-vitals/onTTFB |
| 41 | + |
| 42 | + // logging the skip reason so that we at least can check for that in CI logs |
| 43 | + // eslint-disable-next-line no-console |
| 44 | + console.log('SKIPPING: TTFB is not reported'); |
| 45 | + sentryTest.skip(); |
| 46 | + } |
| 47 | + |
| 48 | + const lcpValue = eventData.measurements?.lcp?.value; |
| 49 | + const fcpValue = eventData.measurements?.fcp?.value; |
| 50 | + const fpValue = eventData.measurements?.fp?.value; |
| 51 | + |
| 52 | + expect(lcpValue).toBeDefined(); |
| 53 | + expect(fcpValue).toBeDefined(); |
| 54 | + expect(fpValue).toBeDefined(); |
| 55 | + |
| 56 | + // (LCP, FCP, FP) >= TTFB |
| 57 | + expect(lcpValue).toBeGreaterThanOrEqual(ttfbValue!); |
| 58 | + expect(fcpValue).toBeGreaterThanOrEqual(ttfbValue!); |
| 59 | + expect(fpValue).toBeGreaterThanOrEqual(ttfbValue!); |
| 60 | +}); |
| 61 | + |
| 62 | +sentryTest('captures time origin as span attribute', async ({ getLocalTestPath, page }) => { |
| 63 | + // Only run in chromium to ensure all vitals are present |
| 64 | + if (shouldSkipTracingTest()) { |
| 65 | + sentryTest.skip(); |
| 66 | + } |
| 67 | + |
| 68 | + const url = await getLocalTestPath({ testDir: __dirname }); |
| 69 | + const [eventData] = await Promise.all([getFirstSentryEnvelopeRequest<Event>(page), page.goto(url)]); |
| 70 | + |
| 71 | + const timeOriginAttribute = eventData.contexts?.trace?.data?.['performance.timeOrigin']; |
| 72 | + const transactionStartTimestamp = eventData.start_timestamp; |
| 73 | + |
| 74 | + expect(timeOriginAttribute).toBeDefined(); |
| 75 | + expect(transactionStartTimestamp).toBeDefined(); |
| 76 | + |
| 77 | + const delta = Math.abs(transactionStartTimestamp! - timeOriginAttribute); |
| 78 | + |
| 79 | + // The delta should be less than 1ms if this flakes, we should increase the threshold |
| 80 | + expect(delta).toBeLessThanOrEqual(1); |
| 81 | +}); |
0 commit comments