|
| 1 | +import { expect, test } from '@playwright/test'; |
| 2 | +import { waitForError, waitForTransaction } from '@sentry-internal/test-utils'; |
| 3 | + |
| 4 | +const APP_NAME = 'hono-4'; |
| 5 | + |
| 6 | +test('creates a span for named middleware', async ({ baseURL }) => { |
| 7 | + const transactionPromise = waitForTransaction(APP_NAME, event => { |
| 8 | + return event.contexts?.trace?.op === 'http.server' && event.transaction === 'GET /test-middleware/named'; |
| 9 | + }); |
| 10 | + |
| 11 | + const response = await fetch(`${baseURL}/test-middleware/named`); |
| 12 | + expect(response.status).toBe(200); |
| 13 | + |
| 14 | + const transaction = await transactionPromise; |
| 15 | + const spans = transaction.spans || []; |
| 16 | + |
| 17 | + const middlewareSpan = spans.find( |
| 18 | + (span: { description?: string; op?: string }) => |
| 19 | + span.op === 'middleware.hono' && span.description === 'middlewareA', |
| 20 | + ); |
| 21 | + |
| 22 | + expect(middlewareSpan).toEqual( |
| 23 | + expect.objectContaining({ |
| 24 | + description: 'middlewareA', |
| 25 | + op: 'middleware.hono', |
| 26 | + origin: 'auto.middleware.hono', |
| 27 | + status: 'ok', |
| 28 | + }), |
| 29 | + ); |
| 30 | + |
| 31 | + // The middleware has a 50ms delay, so the span duration should be at least 50ms (0.05s) |
| 32 | + // @ts-expect-error timestamp is defined |
| 33 | + const durationMs = (middlewareSpan?.timestamp - middlewareSpan?.start_timestamp) * 1000; |
| 34 | + expect(durationMs).toBeGreaterThanOrEqual(50); |
| 35 | +}); |
| 36 | + |
| 37 | +test('creates a span for anonymous middleware', async ({ baseURL }) => { |
| 38 | + const transactionPromise = waitForTransaction(APP_NAME, event => { |
| 39 | + return event.contexts?.trace?.op === 'http.server' && event.transaction === 'GET /test-middleware/anonymous'; |
| 40 | + }); |
| 41 | + |
| 42 | + const response = await fetch(`${baseURL}/test-middleware/anonymous`); |
| 43 | + expect(response.status).toBe(200); |
| 44 | + |
| 45 | + const transaction = await transactionPromise; |
| 46 | + const spans = transaction.spans || []; |
| 47 | + |
| 48 | + expect(spans).toContainEqual( |
| 49 | + expect.objectContaining({ |
| 50 | + description: '<anonymous>', |
| 51 | + op: 'middleware.hono', |
| 52 | + origin: 'auto.middleware.hono', |
| 53 | + status: 'ok', |
| 54 | + }), |
| 55 | + ); |
| 56 | +}); |
| 57 | + |
| 58 | +test('multiple middleware are sibling spans under the same parent', async ({ baseURL }) => { |
| 59 | + const transactionPromise = waitForTransaction(APP_NAME, event => { |
| 60 | + return event.contexts?.trace?.op === 'http.server' && event.transaction === 'GET /test-middleware/multi'; |
| 61 | + }); |
| 62 | + |
| 63 | + const response = await fetch(`${baseURL}/test-middleware/multi`); |
| 64 | + expect(response.status).toBe(200); |
| 65 | + |
| 66 | + const transaction = await transactionPromise; |
| 67 | + const spans = transaction.spans || []; |
| 68 | + |
| 69 | + const middlewareSpans = spans.filter( |
| 70 | + (span: { op?: string; origin?: string }) => span.op === 'middleware.hono' && span.origin === 'auto.middleware.hono', |
| 71 | + ); |
| 72 | + |
| 73 | + expect(middlewareSpans).toHaveLength(2); |
| 74 | + expect(middlewareSpans[0]?.description).toBe('middlewareA'); |
| 75 | + expect(middlewareSpans[1]?.description).toBe('middlewareB'); |
| 76 | + |
| 77 | + // Both middleware spans share the same parent (siblings, not nested) |
| 78 | + expect(middlewareSpans[0]?.parent_span_id).toBe(middlewareSpans[1]?.parent_span_id); |
| 79 | + |
| 80 | + // middlewareA has a 50ms delay, middlewareB has a 60ms delay |
| 81 | + // @ts-expect-error timestamp is defined |
| 82 | + const timestampDurationMs = (middlewareSpans[0]?.timestamp - middlewareSpans[0]?.start_timestamp) * 1000; |
| 83 | + // @ts-expect-error timestamp is defined |
| 84 | + const authDurationMs = (middlewareSpans[1]?.timestamp - middlewareSpans[1]?.start_timestamp) * 1000; |
| 85 | + expect(timestampDurationMs).toBeGreaterThanOrEqual(50); |
| 86 | + expect(authDurationMs).toBeGreaterThanOrEqual(60); |
| 87 | +}); |
| 88 | + |
| 89 | +test('captures error thrown in middleware', async ({ baseURL }) => { |
| 90 | + const errorPromise = waitForError(APP_NAME, event => { |
| 91 | + return event.exception?.values?.[0]?.value === 'Middleware error'; |
| 92 | + }); |
| 93 | + |
| 94 | + const response = await fetch(`${baseURL}/test-middleware/error`); |
| 95 | + expect(response.status).toBe(500); |
| 96 | + |
| 97 | + const errorEvent = await errorPromise; |
| 98 | + expect(errorEvent.exception?.values?.[0]?.value).toBe('Middleware error'); |
| 99 | + expect(errorEvent.exception?.values?.[0]?.mechanism).toEqual( |
| 100 | + expect.objectContaining({ |
| 101 | + handled: false, |
| 102 | + type: 'auto.middleware.hono', |
| 103 | + }), |
| 104 | + ); |
| 105 | +}); |
| 106 | + |
| 107 | +test('sets error status on middleware span when middleware throws', async ({ baseURL }) => { |
| 108 | + const transactionPromise = waitForTransaction(APP_NAME, event => { |
| 109 | + return event.contexts?.trace?.op === 'http.server' && event.transaction === 'GET /test-middleware/error/*'; |
| 110 | + }); |
| 111 | + |
| 112 | + await fetch(`${baseURL}/test-middleware/error`); |
| 113 | + |
| 114 | + const transaction = await transactionPromise; |
| 115 | + const spans = transaction.spans || []; |
| 116 | + |
| 117 | + const failingSpan = spans.find( |
| 118 | + (span: { description?: string; op?: string }) => |
| 119 | + span.op === 'middleware.hono' && span.description === 'failingMiddleware', |
| 120 | + ); |
| 121 | + |
| 122 | + expect(failingSpan).toBeDefined(); |
| 123 | + expect(failingSpan?.status).toBe('internal_error'); |
| 124 | + expect(failingSpan?.origin).toBe('auto.middleware.hono'); |
| 125 | +}); |
| 126 | + |
| 127 | +test('includes request data on error events from middleware', async ({ baseURL }) => { |
| 128 | + const errorPromise = waitForError(APP_NAME, event => { |
| 129 | + return event.exception?.values?.[0]?.value === 'Middleware error'; |
| 130 | + }); |
| 131 | + |
| 132 | + await fetch(`${baseURL}/test-middleware/error`); |
| 133 | + |
| 134 | + const errorEvent = await errorPromise; |
| 135 | + expect(errorEvent.request).toEqual( |
| 136 | + expect.objectContaining({ |
| 137 | + method: 'GET', |
| 138 | + url: expect.stringContaining('/test-middleware/error'), |
| 139 | + }), |
| 140 | + ); |
| 141 | +}); |
0 commit comments