|
| 1 | +import * as SentryCore from '@sentry/core'; |
| 2 | +import { Hono } from 'hono'; |
| 3 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 4 | +import { patchAppUse } from '../../src/shared/patchAppUse'; |
| 5 | + |
| 6 | +vi.mock('@sentry/core', async () => { |
| 7 | + const actual = await vi.importActual('@sentry/core'); |
| 8 | + return { |
| 9 | + ...actual, |
| 10 | + startInactiveSpan: vi.fn((_opts: unknown) => ({ |
| 11 | + setStatus: vi.fn(), |
| 12 | + end: vi.fn(), |
| 13 | + })), |
| 14 | + captureException: vi.fn(), |
| 15 | + }; |
| 16 | +}); |
| 17 | + |
| 18 | +const startInactiveSpanMock = SentryCore.startInactiveSpan as ReturnType<typeof vi.fn>; |
| 19 | +const captureExceptionMock = SentryCore.captureException as ReturnType<typeof vi.fn>; |
| 20 | + |
| 21 | +describe('patchAppUse (middleware spans)', () => { |
| 22 | + beforeEach(() => { |
| 23 | + vi.clearAllMocks(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('wraps handlers in app.use(handler) so startInactiveSpan is called when middleware runs', async () => { |
| 27 | + const app = new Hono(); |
| 28 | + patchAppUse(app); |
| 29 | + |
| 30 | + const userHandler = vi.fn(async (_c: unknown, next: () => Promise<void>) => { |
| 31 | + await next(); |
| 32 | + }); |
| 33 | + app.use(userHandler); |
| 34 | + |
| 35 | + expect(startInactiveSpanMock).not.toHaveBeenCalled(); |
| 36 | + |
| 37 | + const fetchHandler = app.fetch; |
| 38 | + const req = new Request('http://localhost/'); |
| 39 | + await fetchHandler(req); |
| 40 | + |
| 41 | + expect(startInactiveSpanMock).toHaveBeenCalledTimes(1); |
| 42 | + expect(startInactiveSpanMock).toHaveBeenCalledWith( |
| 43 | + expect.objectContaining({ |
| 44 | + op: 'middleware.hono', |
| 45 | + onlyIfParent: true, |
| 46 | + attributes: expect.objectContaining({ |
| 47 | + 'sentry.op': 'middleware.hono', |
| 48 | + 'sentry.origin': 'auto.middleware.hono', |
| 49 | + }), |
| 50 | + }), |
| 51 | + ); |
| 52 | + expect(userHandler).toHaveBeenCalled(); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('span naming', () => { |
| 56 | + it('uses handler.name for span when handler has a name', async () => { |
| 57 | + const app = new Hono(); |
| 58 | + patchAppUse(app); |
| 59 | + |
| 60 | + async function myNamedMiddleware(_c: unknown, next: () => Promise<void>) { |
| 61 | + await next(); |
| 62 | + } |
| 63 | + app.use(myNamedMiddleware); |
| 64 | + |
| 65 | + await app.fetch(new Request('http://localhost/')); |
| 66 | + |
| 67 | + expect(startInactiveSpanMock).toHaveBeenCalledWith(expect.objectContaining({ name: 'myNamedMiddleware' })); |
| 68 | + }); |
| 69 | + |
| 70 | + it('uses <anonymous.index> for span when handler is anonymous', async () => { |
| 71 | + const app = new Hono(); |
| 72 | + patchAppUse(app); |
| 73 | + |
| 74 | + app.use(async (_c: unknown, next: () => Promise<void>) => next()); |
| 75 | + |
| 76 | + await app.fetch(new Request('http://localhost/')); |
| 77 | + |
| 78 | + expect(startInactiveSpanMock).toHaveBeenCalledTimes(1); |
| 79 | + const name = startInactiveSpanMock.mock.calls[0][0].name; |
| 80 | + expect(name).toMatch('<anonymous>'); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + it('wraps each handler in app.use(path, ...handlers) and passes path through', async () => { |
| 85 | + const app = new Hono(); |
| 86 | + patchAppUse(app); |
| 87 | + |
| 88 | + const handler = async (_c: unknown, next: () => Promise<void>) => next(); |
| 89 | + app.use('/api', handler); |
| 90 | + app.get('/api', () => new Response('ok')); |
| 91 | + |
| 92 | + await app.fetch(new Request('http://localhost/api')); |
| 93 | + |
| 94 | + expect(startInactiveSpanMock).toHaveBeenCalled(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('calls captureException when middleware throws', async () => { |
| 98 | + const app = new Hono(); |
| 99 | + patchAppUse(app); |
| 100 | + |
| 101 | + const err = new Error('middleware error'); |
| 102 | + app.use(async () => { |
| 103 | + throw err; |
| 104 | + }); |
| 105 | + |
| 106 | + const res = await app.fetch(new Request('http://localhost/')); |
| 107 | + expect(res.status).toBe(500); |
| 108 | + |
| 109 | + expect(captureExceptionMock).toHaveBeenCalledWith(err, { |
| 110 | + mechanism: { handled: false, type: 'auto.middleware.hono' }, |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + it('creates sibling spans for multiple middlewares (onion order, not parent-child)', async () => { |
| 115 | + const app = new Hono(); |
| 116 | + patchAppUse(app); |
| 117 | + |
| 118 | + app.use( |
| 119 | + async (_c: unknown, next: () => Promise<void>) => next(), |
| 120 | + async function namedMiddleware(_c: unknown, next: () => Promise<void>) { |
| 121 | + await next(); |
| 122 | + }, |
| 123 | + async (_c: unknown, next: () => Promise<void>) => next(), |
| 124 | + ); |
| 125 | + |
| 126 | + await app.fetch(new Request('http://localhost/')); |
| 127 | + |
| 128 | + expect(startInactiveSpanMock).toHaveBeenCalledTimes(3); |
| 129 | + const [firstCall, secondCall, thirdCall] = startInactiveSpanMock.mock.calls; |
| 130 | + expect(firstCall[0]).toMatchObject({ op: 'middleware.hono' }); |
| 131 | + expect(secondCall[0]).toMatchObject({ op: 'middleware.hono' }); |
| 132 | + expect(firstCall[0].name).toMatch('<anonymous>'); |
| 133 | + expect(secondCall[0].name).toBe('namedMiddleware'); |
| 134 | + expect(thirdCall[0].name).toBe('<anonymous>'); |
| 135 | + expect(firstCall[0].name).not.toBe(secondCall[0].name); |
| 136 | + }); |
| 137 | + |
| 138 | + it('preserves this context when calling the original use (Proxy forwards thisArg)', () => { |
| 139 | + type FakeApp = { |
| 140 | + _capturedThis: unknown; |
| 141 | + use: (...args: unknown[]) => FakeApp; |
| 142 | + }; |
| 143 | + const fakeApp: FakeApp = { |
| 144 | + _capturedThis: null, |
| 145 | + use(this: FakeApp, ..._args: unknown[]) { |
| 146 | + this._capturedThis = this; |
| 147 | + return this; |
| 148 | + }, |
| 149 | + }; |
| 150 | + |
| 151 | + patchAppUse(fakeApp as unknown as Parameters<typeof patchAppUse>[0]); |
| 152 | + |
| 153 | + const noop = async (_c: unknown, next: () => Promise<void>) => next(); |
| 154 | + fakeApp.use(noop); |
| 155 | + |
| 156 | + expect(fakeApp._capturedThis).toBe(fakeApp); |
| 157 | + }); |
| 158 | +}); |
0 commit comments