|
| 1 | +import { expect, test } from '@playwright/test'; |
| 2 | +import { waitForError, waitForTransaction } from '@sentry-internal/test-utils'; |
| 3 | + |
| 4 | +test('Should create a transaction for middleware', async ({ request }) => { |
| 5 | + const middlewareTransactionPromise = waitForTransaction('nextjs-16', async transactionEvent => { |
| 6 | + return transactionEvent?.transaction === 'middleware GET'; |
| 7 | + }); |
| 8 | + |
| 9 | + const response = await request.get('/api/endpoint-behind-middleware'); |
| 10 | + expect(await response.json()).toStrictEqual({ name: 'John Doe' }); |
| 11 | + |
| 12 | + const middlewareTransaction = await middlewareTransactionPromise; |
| 13 | + |
| 14 | + expect(middlewareTransaction.contexts?.trace?.status).toBe('ok'); |
| 15 | + expect(middlewareTransaction.contexts?.trace?.op).toBe('http.server.middleware'); |
| 16 | + expect(middlewareTransaction.contexts?.runtime?.name).toBe('vercel-edge'); |
| 17 | + expect(middlewareTransaction.transaction_info?.source).toBe('url'); |
| 18 | + |
| 19 | + // Assert that isolation scope works properly |
| 20 | + expect(middlewareTransaction.tags?.['my-isolated-tag']).toBe(true); |
| 21 | + expect(middlewareTransaction.tags?.['my-global-scope-isolated-tag']).not.toBeDefined(); |
| 22 | +}); |
| 23 | + |
| 24 | +test('Faulty middlewares', async ({ request }) => { |
| 25 | + const middlewareTransactionPromise = waitForTransaction('nextjs-16', async transactionEvent => { |
| 26 | + return transactionEvent?.transaction === 'middleware GET'; |
| 27 | + }); |
| 28 | + |
| 29 | + const errorEventPromise = waitForError('nextjs-16', errorEvent => { |
| 30 | + return errorEvent?.exception?.values?.[0]?.value === 'Middleware Error'; |
| 31 | + }); |
| 32 | + |
| 33 | + request.get('/api/endpoint-behind-middleware', { headers: { 'x-should-throw': '1' } }).catch(() => { |
| 34 | + // Noop |
| 35 | + }); |
| 36 | + |
| 37 | + await test.step('should record transactions', async () => { |
| 38 | + const middlewareTransaction = await middlewareTransactionPromise; |
| 39 | + expect(middlewareTransaction.contexts?.trace?.status).toBe('unknown_error'); |
| 40 | + expect(middlewareTransaction.contexts?.trace?.op).toBe('http.server.middleware'); |
| 41 | + expect(middlewareTransaction.contexts?.runtime?.name).toBe('vercel-edge'); |
| 42 | + expect(middlewareTransaction.transaction_info?.source).toBe('url'); |
| 43 | + }); |
| 44 | + |
| 45 | + await test.step('should record exceptions', async () => { |
| 46 | + const errorEvent = await errorEventPromise; |
| 47 | + |
| 48 | + // Assert that isolation scope works properly |
| 49 | + expect(errorEvent.tags?.['my-isolated-tag']).toBe(true); |
| 50 | + expect(errorEvent.tags?.['my-global-scope-isolated-tag']).not.toBeDefined(); |
| 51 | + console.log('errorEvent', errorEvent.transaction); |
| 52 | + expect(errorEvent.transaction).toBe('middleware GET'); |
| 53 | + }); |
| 54 | +}); |
| 55 | + |
| 56 | +test('Should trace outgoing fetch requests inside middleware and create breadcrumbs for it', async ({ request }) => { |
| 57 | + const middlewareTransactionPromise = waitForTransaction('nextjs-16', async transactionEvent => { |
| 58 | + return ( |
| 59 | + transactionEvent?.transaction === 'middleware GET /api/endpoint-behind-middleware' && |
| 60 | + !!transactionEvent.spans?.find(span => span.op === 'http.client') |
| 61 | + ); |
| 62 | + }); |
| 63 | + |
| 64 | + request.get('/api/endpoint-behind-middleware', { headers: { 'x-should-make-request': '1' } }).catch(() => { |
| 65 | + // Noop |
| 66 | + }); |
| 67 | + |
| 68 | + const middlewareTransaction = await middlewareTransactionPromise; |
| 69 | + |
| 70 | + expect(middlewareTransaction.spans).toEqual( |
| 71 | + expect.arrayContaining([ |
| 72 | + { |
| 73 | + data: { |
| 74 | + 'http.method': 'GET', |
| 75 | + 'http.response.status_code': 200, |
| 76 | + type: 'fetch', |
| 77 | + url: 'http://localhost:3030/', |
| 78 | + 'http.url': 'http://localhost:3030/', |
| 79 | + 'server.address': 'localhost:3030', |
| 80 | + 'sentry.op': 'http.client', |
| 81 | + 'sentry.origin': 'auto.http.wintercg_fetch', |
| 82 | + }, |
| 83 | + description: 'GET http://localhost:3030/', |
| 84 | + op: 'http.client', |
| 85 | + origin: 'auto.http.wintercg_fetch', |
| 86 | + parent_span_id: expect.stringMatching(/[a-f0-9]{16}/), |
| 87 | + span_id: expect.stringMatching(/[a-f0-9]{16}/), |
| 88 | + start_timestamp: expect.any(Number), |
| 89 | + status: 'ok', |
| 90 | + timestamp: expect.any(Number), |
| 91 | + trace_id: expect.stringMatching(/[a-f0-9]{32}/), |
| 92 | + }, |
| 93 | + ]), |
| 94 | + ); |
| 95 | + expect(middlewareTransaction.breadcrumbs).toEqual( |
| 96 | + expect.arrayContaining([ |
| 97 | + { |
| 98 | + category: 'fetch', |
| 99 | + data: { method: 'GET', status_code: 200, url: 'http://localhost:3030/' }, |
| 100 | + timestamp: expect.any(Number), |
| 101 | + type: 'http', |
| 102 | + }, |
| 103 | + ]), |
| 104 | + ); |
| 105 | +}); |
0 commit comments