|
| 1 | +import * as SentryCore from '@sentry/core'; |
| 2 | +import { SDK_VERSION } from '@sentry/core'; |
| 3 | +import { Hono } from 'hono'; |
| 4 | +import { beforeEach, describe, expect, it, type Mock, vi } from 'vitest'; |
| 5 | +import { sentry } from '../../src/node/middleware'; |
| 6 | + |
| 7 | +vi.mock('@sentry/node', () => ({ |
| 8 | + init: vi.fn(), |
| 9 | +})); |
| 10 | + |
| 11 | +// eslint-disable-next-line @typescript-eslint/consistent-type-imports |
| 12 | +const { init: initNodeMock } = await vi.importMock<typeof import('@sentry/node')>('@sentry/node'); |
| 13 | + |
| 14 | +vi.mock('@sentry/core', async () => { |
| 15 | + const actual = await vi.importActual('@sentry/core'); |
| 16 | + return { |
| 17 | + ...actual, |
| 18 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 19 | + // @ts-ignore |
| 20 | + applySdkMetadata: vi.fn(actual.applySdkMetadata), |
| 21 | + }; |
| 22 | +}); |
| 23 | + |
| 24 | +const applySdkMetadataMock = SentryCore.applySdkMetadata as Mock; |
| 25 | + |
| 26 | +describe('Hono Vercel Middleware', () => { |
| 27 | + beforeEach(() => { |
| 28 | + vi.clearAllMocks(); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('sentry middleware', () => { |
| 32 | + it('calls applySdkMetadata with "hono"', () => { |
| 33 | + const app = new Hono(); |
| 34 | + const options = { |
| 35 | + dsn: 'https://public@dsn.ingest.sentry.io/1337', |
| 36 | + }; |
| 37 | + |
| 38 | + sentry(app, options); |
| 39 | + |
| 40 | + expect(applySdkMetadataMock).toHaveBeenCalledTimes(1); |
| 41 | + expect(applySdkMetadataMock).toHaveBeenCalledWith(options, 'hono'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('calls init from @sentry/node', () => { |
| 45 | + const app = new Hono(); |
| 46 | + const options = { |
| 47 | + dsn: 'https://public@dsn.ingest.sentry.io/1337', |
| 48 | + }; |
| 49 | + |
| 50 | + sentry(app, options); |
| 51 | + |
| 52 | + expect(initNodeMock).toHaveBeenCalledTimes(1); |
| 53 | + expect(initNodeMock).toHaveBeenCalledWith( |
| 54 | + expect.objectContaining({ |
| 55 | + dsn: 'https://public@dsn.ingest.sentry.io/1337', |
| 56 | + }), |
| 57 | + ); |
| 58 | + }); |
| 59 | + |
| 60 | + it('sets SDK metadata before calling Node init', () => { |
| 61 | + const app = new Hono(); |
| 62 | + const options = { |
| 63 | + dsn: 'https://public@dsn.ingest.sentry.io/1337', |
| 64 | + }; |
| 65 | + |
| 66 | + sentry(app, options); |
| 67 | + |
| 68 | + const applySdkMetadataCallOrder = applySdkMetadataMock.mock.invocationCallOrder[0]; |
| 69 | + const initNodeCallOrder = (initNodeMock as Mock).mock.invocationCallOrder[0]; |
| 70 | + |
| 71 | + expect(applySdkMetadataCallOrder).toBeLessThan(initNodeCallOrder as number); |
| 72 | + }); |
| 73 | + |
| 74 | + it('preserves all user options', () => { |
| 75 | + const app = new Hono(); |
| 76 | + const options = { |
| 77 | + dsn: 'https://public@dsn.ingest.sentry.io/1337', |
| 78 | + environment: 'production', |
| 79 | + sampleRate: 0.5, |
| 80 | + tracesSampleRate: 1.0, |
| 81 | + debug: true, |
| 82 | + }; |
| 83 | + |
| 84 | + sentry(app, options); |
| 85 | + |
| 86 | + expect(initNodeMock).toHaveBeenCalledWith( |
| 87 | + expect.objectContaining({ |
| 88 | + dsn: 'https://public@dsn.ingest.sentry.io/1337', |
| 89 | + environment: 'production', |
| 90 | + sampleRate: 0.5, |
| 91 | + tracesSampleRate: 1.0, |
| 92 | + debug: true, |
| 93 | + }), |
| 94 | + ); |
| 95 | + }); |
| 96 | + |
| 97 | + it('returns a middleware handler function', () => { |
| 98 | + const app = new Hono(); |
| 99 | + const options = { |
| 100 | + dsn: 'https://public@dsn.ingest.sentry.io/1337', |
| 101 | + }; |
| 102 | + |
| 103 | + const middleware = sentry(app, options); |
| 104 | + |
| 105 | + expect(middleware).toBeDefined(); |
| 106 | + expect(typeof middleware).toBe('function'); |
| 107 | + expect(middleware).toHaveLength(2); // Hono middleware takes (context, next) |
| 108 | + }); |
| 109 | + |
| 110 | + it('returns an async middleware handler', () => { |
| 111 | + const app = new Hono(); |
| 112 | + const middleware = sentry(app, {}); |
| 113 | + |
| 114 | + expect(middleware.constructor.name).toBe('AsyncFunction'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('includes hono SDK metadata', () => { |
| 118 | + const app = new Hono(); |
| 119 | + const options = { |
| 120 | + dsn: 'https://public@dsn.ingest.sentry.io/1337', |
| 121 | + }; |
| 122 | + |
| 123 | + sentry(app, options); |
| 124 | + |
| 125 | + expect(initNodeMock).toHaveBeenCalledWith( |
| 126 | + expect.objectContaining({ |
| 127 | + _metadata: expect.objectContaining({ |
| 128 | + sdk: expect.objectContaining({ |
| 129 | + name: 'sentry.javascript.hono', |
| 130 | + version: SDK_VERSION, |
| 131 | + packages: [{ name: 'npm:@sentry/hono', version: SDK_VERSION }], |
| 132 | + }), |
| 133 | + }), |
| 134 | + }), |
| 135 | + ); |
| 136 | + }); |
| 137 | + }); |
| 138 | +}); |
0 commit comments