|
| 1 | +import { expect, test } from '@playwright/test'; |
| 2 | +import { waitForError, waitForTransaction } from '@sentry-internal/test-utils'; |
| 3 | + |
| 4 | +test('Should create a transaction for edge routes', async ({ request }) => { |
| 5 | + const edgerouteTransactionPromise = waitForTransaction('nextjs-app-dir', async transactionEvent => { |
| 6 | + return ( |
| 7 | + transactionEvent?.transaction === 'GET /api/edge-endpoint' && |
| 8 | + transactionEvent.contexts?.runtime?.name === 'vercel-edge' |
| 9 | + ); |
| 10 | + }); |
| 11 | + |
| 12 | + const response = await request.get('/api/edge-endpoint', { |
| 13 | + headers: { |
| 14 | + 'x-yeet': 'test-value', |
| 15 | + }, |
| 16 | + }); |
| 17 | + expect(await response.json()).toStrictEqual({ name: 'Jim Halpert' }); |
| 18 | + |
| 19 | + const edgerouteTransaction = await edgerouteTransactionPromise; |
| 20 | + |
| 21 | + expect(edgerouteTransaction.contexts?.trace?.status).toBe('ok'); |
| 22 | + expect(edgerouteTransaction.contexts?.trace?.op).toBe('http.server'); |
| 23 | + expect(edgerouteTransaction.request?.headers?.['x-yeet']).toBe('test-value'); |
| 24 | +}); |
| 25 | + |
| 26 | +test('Faulty edge routes', async ({ request }) => { |
| 27 | + const edgerouteTransactionPromise = waitForTransaction('nextjs-app-dir', async transactionEvent => { |
| 28 | + return ( |
| 29 | + transactionEvent?.transaction === 'GET /api/error-edge-endpoint' && |
| 30 | + transactionEvent.contexts?.runtime?.name === 'vercel-edge' |
| 31 | + ); |
| 32 | + }); |
| 33 | + |
| 34 | + const errorEventPromise = waitForError('nextjs-app-dir', errorEvent => { |
| 35 | + return ( |
| 36 | + errorEvent?.exception?.values?.[0]?.value === 'Edge Route Error' && |
| 37 | + errorEvent.contexts?.runtime?.name === 'vercel-edge' |
| 38 | + ); |
| 39 | + }); |
| 40 | + |
| 41 | + request.get('/api/error-edge-endpoint').catch(() => { |
| 42 | + // Noop |
| 43 | + }); |
| 44 | + |
| 45 | + const [edgerouteTransaction, errorEvent] = await Promise.all([ |
| 46 | + test.step('should create a transaction', () => edgerouteTransactionPromise), |
| 47 | + test.step('should create an error event', () => errorEventPromise), |
| 48 | + ]); |
| 49 | + |
| 50 | + test.step('should create transactions with the right fields', () => { |
| 51 | + expect(edgerouteTransaction.contexts?.trace?.status).toBe('unknown_error'); |
| 52 | + expect(edgerouteTransaction.contexts?.trace?.op).toBe('http.server'); |
| 53 | + }); |
| 54 | + |
| 55 | + test.step('should have scope isolation', () => { |
| 56 | + expect(edgerouteTransaction.tags?.['my-isolated-tag']).toBe(true); |
| 57 | + expect(edgerouteTransaction.tags?.['my-global-scope-isolated-tag']).not.toBeDefined(); |
| 58 | + expect(errorEvent.tags?.['my-isolated-tag']).toBe(true); |
| 59 | + expect(errorEvent.tags?.['my-global-scope-isolated-tag']).not.toBeDefined(); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +test('Should not create spans for outgoing Sentry requests on edge routes', async ({ request }) => { |
| 64 | + // Ensure no http.client transaction is created for any orphaned request |
| 65 | + waitForTransaction('nextjs-app-dir', async transactionEvent => { |
| 66 | + if (transactionEvent.contexts?.trace?.op === 'http.client') { |
| 67 | + throw new Error(`Should not receive http.client transaction, but got: ${transactionEvent.transaction}`); |
| 68 | + } |
| 69 | + return false; |
| 70 | + }); |
| 71 | + |
| 72 | + // We hit the endpoint three times and check that nowhere a http.client span for Sentry is to be found |
| 73 | + // this way, we ensure that nothing is sent to Sentry in a follow up span |
| 74 | + const edgerouteTransactionPromise1 = waitForTransaction('nextjs-app-dir', async transactionEvent => { |
| 75 | + return ( |
| 76 | + transactionEvent?.transaction === 'GET /api/edge-endpoint' && |
| 77 | + transactionEvent.contexts?.runtime?.name === 'vercel-edge' |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
| 81 | + await request.get('/api/edge-endpoint', { |
| 82 | + headers: { |
| 83 | + 'x-yeet': 'test-value', |
| 84 | + }, |
| 85 | + }); |
| 86 | + |
| 87 | + const edgerouteTransactionPromise2 = waitForTransaction('nextjs-app-dir', async transactionEvent => { |
| 88 | + return ( |
| 89 | + transactionEvent?.transaction === 'GET /api/edge-endpoint' && |
| 90 | + transactionEvent.contexts?.runtime?.name === 'vercel-edge' |
| 91 | + ); |
| 92 | + }); |
| 93 | + |
| 94 | + await request.get('/api/edge-endpoint', { |
| 95 | + headers: { |
| 96 | + 'x-yeet': 'test-value-2', |
| 97 | + }, |
| 98 | + }); |
| 99 | + |
| 100 | + const edgerouteTransactionPromise3 = waitForTransaction('nextjs-app-dir', async transactionEvent => { |
| 101 | + return ( |
| 102 | + transactionEvent?.transaction === 'GET /api/edge-endpoint' && |
| 103 | + transactionEvent.contexts?.runtime?.name === 'vercel-edge' |
| 104 | + ); |
| 105 | + }); |
| 106 | + |
| 107 | + await request.get('/api/edge-endpoint', { |
| 108 | + headers: { |
| 109 | + 'x-yeet': 'test-value-3', |
| 110 | + }, |
| 111 | + }); |
| 112 | + |
| 113 | + const [edgerouteTransaction1, edgerouteTransaction2, edgerouteTransaction3] = await Promise.all([ |
| 114 | + edgerouteTransactionPromise1, |
| 115 | + edgerouteTransactionPromise2, |
| 116 | + edgerouteTransactionPromise3, |
| 117 | + ]); |
| 118 | + |
| 119 | + expect(edgerouteTransaction1.contexts?.trace?.op).toBe('http.server'); |
| 120 | + expect(edgerouteTransaction2.contexts?.trace?.op).toBe('http.server'); |
| 121 | + expect(edgerouteTransaction3.contexts?.trace?.op).toBe('http.server'); |
| 122 | + |
| 123 | + expect(edgerouteTransaction1.spans?.length).toBe(1); |
| 124 | + expect(edgerouteTransaction2.spans?.length).toBe(1); |
| 125 | + expect(edgerouteTransaction3.spans?.length).toBe(1); |
| 126 | + |
| 127 | + expect(edgerouteTransaction1.spans?.[0].description).toBe('handler (/api/edge-endpoint)'); |
| 128 | + expect(edgerouteTransaction2.spans?.[0].description).toBe('handler (/api/edge-endpoint)'); |
| 129 | + expect(edgerouteTransaction3.spans?.[0].description).toBe('handler (/api/edge-endpoint)'); |
| 130 | +}); |
0 commit comments