|
| 1 | +import type { CloudflareOptions } from '@sentry/cloudflare'; |
| 2 | +import { wrapRequestHandler } from '@sentry/cloudflare/request'; |
| 3 | +import type { NitroApp } from 'nitropack'; |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 5 | +import { sentryCloudflareNitroPlugin } from '../../../src/runtime/plugins/sentry-cloudflare.server'; |
| 6 | + |
| 7 | +vi.mock('@sentry/cloudflare/request', () => ({ |
| 8 | + wrapRequestHandler: vi.fn((_options: unknown, callback: () => unknown) => callback()), |
| 9 | +})); |
| 10 | + |
| 11 | +const DSN = 'https://public@dsn.ingest.sentry.io/1337'; |
| 12 | + |
| 13 | +async function handleRequest(options: CloudflareOptions): Promise<void> { |
| 14 | + const nitroApp = { localFetch: vi.fn(), hooks: { hook: vi.fn() } } as unknown as NitroApp; |
| 15 | + sentryCloudflareNitroPlugin(options)(nitroApp); |
| 16 | + |
| 17 | + const event = { |
| 18 | + protocol: 'https:', |
| 19 | + host: 'example.com', |
| 20 | + method: 'GET', |
| 21 | + headers: {}, |
| 22 | + context: { cf: {}, cloudflare: { context: {} } }, |
| 23 | + }; |
| 24 | + await nitroApp.localFetch('/api/db-mysql', event as unknown as RequestInit); |
| 25 | +} |
| 26 | + |
| 27 | +describe('sentryCloudflareNitroPlugin', () => { |
| 28 | + beforeEach(() => { |
| 29 | + vi.mocked(wrapRequestHandler).mockClear(); |
| 30 | + }); |
| 31 | + |
| 32 | + it('passes a defaultIntegrations list that extends the SDK defaults', async () => { |
| 33 | + await handleRequest({ dsn: DSN }); |
| 34 | + |
| 35 | + expect(wrapRequestHandler).toHaveBeenCalledWith( |
| 36 | + expect.objectContaining({ |
| 37 | + options: expect.objectContaining({ |
| 38 | + defaultIntegrations: expect.any(Array), |
| 39 | + extendsDefaultIntegrations: true, |
| 40 | + }), |
| 41 | + }), |
| 42 | + expect.any(Function), |
| 43 | + ); |
| 44 | + }); |
| 45 | + |
| 46 | + it('does not mark a defaultIntegrations list the user passed as extending the SDK defaults', async () => { |
| 47 | + await handleRequest({ dsn: DSN, defaultIntegrations: [] }); |
| 48 | + |
| 49 | + expect(wrapRequestHandler).toHaveBeenCalledWith( |
| 50 | + expect.objectContaining({ |
| 51 | + options: expect.objectContaining({ defaultIntegrations: [], extendsDefaultIntegrations: false }), |
| 52 | + }), |
| 53 | + expect.any(Function), |
| 54 | + ); |
| 55 | + }); |
| 56 | +}); |
0 commit comments