|
| 1 | +import { DurableObject, WorkerEntrypoint, WorkflowEntrypoint } from 'cloudflare:workers'; |
| 2 | +import { describe, it } from 'vitest'; |
| 3 | +import type { CloudflareOptions } from '../src/client'; |
| 4 | +import { defineCloudflareOptions } from '../src/defineCloudflareOptions'; |
| 5 | +import { instrumentAgentWithSentry, instrumentDurableObjectWithSentry } from '../src/durableobject'; |
| 6 | +import { instrumentWorkerEntrypoint } from '../src/instrumentations/instrumentWorkerEntrypoint'; |
| 7 | +import { sentryPagesPlugin } from '../src/pages-plugin'; |
| 8 | +import { withSentry } from '../src/withSentry'; |
| 9 | +import { instrumentWorkflowWithSentry } from '../src/workflows'; |
| 10 | + |
| 11 | +interface TestEnv { |
| 12 | + SENTRY_DSN: string; |
| 13 | +} |
| 14 | + |
| 15 | +const dsn = 'https://public@dsn.ingest.sentry.io/1337'; |
| 16 | + |
| 17 | +const handler = { |
| 18 | + fetch(): Response { |
| 19 | + return new Response('ok'); |
| 20 | + }, |
| 21 | +} satisfies ExportedHandler<TestEnv>; |
| 22 | + |
| 23 | +class TestDurableObject extends DurableObject<TestEnv> {} |
| 24 | + |
| 25 | +class TestWorkerEntrypoint extends WorkerEntrypoint<TestEnv> { |
| 26 | + public ping(): string { |
| 27 | + return 'pong'; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +class TestWorkflow extends WorkflowEntrypoint<TestEnv> { |
| 32 | + public async run(): Promise<void> {} |
| 33 | +} |
| 34 | + |
| 35 | +declare const flag: boolean; |
| 36 | +declare const preTypedOptions: CloudflareOptions; |
| 37 | +declare const makeOptions: () => CloudflareOptions; |
| 38 | + |
| 39 | +// The options callback returns a *fresh* object literal across a function boundary, where |
| 40 | +// TypeScript's excess property check does not reach. `StrictCloudflareOptions` restores it — |
| 41 | +// without these assertions a typo like `tracesSampleRte` silently compiles. |
| 42 | +// |
| 43 | +// Keep each asserted call on a single line: the formatter wraps longer calls and would move |
| 44 | +// the `@ts-expect-error` directive away from the line the error is reported on. |
| 45 | +describe('options are checked for unknown keys', () => { |
| 46 | + it('rejects an unknown key alongside valid ones', () => { |
| 47 | + // @ts-expect-error - `wrongKey` is not a CloudflareOptions key |
| 48 | + withSentry(env => ({ dsn: env.SENTRY_DSN, wrongKey: 123 }), handler); |
| 49 | + }); |
| 50 | + |
| 51 | + it('rejects an options object where every key is unknown', () => { |
| 52 | + // @ts-expect-error - `tracesSampleRte` is a typo for `tracesSampleRate` |
| 53 | + withSentry(() => ({ tracesSampleRte: 1 }), handler); |
| 54 | + }); |
| 55 | + |
| 56 | + it('rejects a wrong value type on a known key', () => { |
| 57 | + // @ts-expect-error - `tracesSampleRate` is a number |
| 58 | + withSentry(() => ({ dsn, tracesSampleRate: 'high' }), handler); |
| 59 | + }); |
| 60 | + |
| 61 | + it('rejects a callback returning a function instead of options', () => { |
| 62 | + // @ts-expect-error - the options factory was returned instead of called |
| 63 | + withSentry(() => makeOptions, handler); |
| 64 | + }); |
| 65 | + |
| 66 | + it('rejects unknown keys in instrumentDurableObjectWithSentry', () => { |
| 67 | + // @ts-expect-error - `wrongKey` is not a CloudflareOptions key |
| 68 | + instrumentDurableObjectWithSentry(() => ({ dsn, wrongKey: 1 }), TestDurableObject); |
| 69 | + }); |
| 70 | + |
| 71 | + it('rejects unknown keys in instrumentAgentWithSentry', () => { |
| 72 | + // @ts-expect-error - `wrongKey` is not a CloudflareOptions key |
| 73 | + instrumentAgentWithSentry(() => ({ dsn, wrongKey: 1 }), TestDurableObject); |
| 74 | + }); |
| 75 | + |
| 76 | + it('rejects unknown keys in instrumentWorkerEntrypoint', () => { |
| 77 | + // @ts-expect-error - `wrongKey` is not a CloudflareOptions key |
| 78 | + instrumentWorkerEntrypoint(() => ({ dsn, wrongKey: 1 }), TestWorkerEntrypoint); |
| 79 | + }); |
| 80 | + |
| 81 | + it('rejects unknown keys in instrumentWorkflowWithSentry', () => { |
| 82 | + // @ts-expect-error - `wrongKey` is not a CloudflareOptions key |
| 83 | + instrumentWorkflowWithSentry(() => ({ dsn, wrongKey: 1 }), TestWorkflow); |
| 84 | + }); |
| 85 | + |
| 86 | + it('rejects unknown keys in defineCloudflareOptions', () => { |
| 87 | + // @ts-expect-error - `wrongKey` is not a CloudflareOptions key |
| 88 | + defineCloudflareOptions(() => ({ dsn, wrongKey: 1 })); |
| 89 | + // @ts-expect-error - `wrongKey` is not a CloudflareOptions key |
| 90 | + defineCloudflareOptions({ dsn, wrongKey: 1 }); |
| 91 | + }); |
| 92 | + |
| 93 | + it('rejects unknown keys in sentryPagesPlugin', () => { |
| 94 | + // @ts-expect-error - `wrongKey` is not a CloudflareOptions key |
| 95 | + sentryPagesPlugin(() => ({ dsn, wrongKey: 1 })); |
| 96 | + // @ts-expect-error - `wrongKey` is not a CloudflareOptions key |
| 97 | + sentryPagesPlugin({ dsn, wrongKey: 1 }); |
| 98 | + }); |
| 99 | +}); |
| 100 | + |
| 101 | +describe('valid options keep compiling', () => { |
| 102 | + it('accepts known keys, including Cloudflare-specific ones', () => { |
| 103 | + withSentry( |
| 104 | + env => ({ |
| 105 | + dsn: env.SENTRY_DSN, |
| 106 | + tracesSampleRate: 1, |
| 107 | + serverName: 'my-worker', |
| 108 | + enableRpcTracePropagation: false, |
| 109 | + durableObjectSqlSpanAllowlist: ['cf_my_table', /^cf_reports_/], |
| 110 | + beforeSend: event => event, |
| 111 | + integrations: [], |
| 112 | + }), |
| 113 | + handler, |
| 114 | + ); |
| 115 | + }); |
| 116 | + |
| 117 | + it('accepts arbitrary keys under `_experiments`', () => { |
| 118 | + withSentry(() => ({ _experiments: { someExperimentalFlag: true } }), handler); |
| 119 | + }); |
| 120 | + |
| 121 | + it('accepts an undefined return, conditional or not', () => { |
| 122 | + withSentry(() => undefined, handler); |
| 123 | + withSentry(() => (flag ? { dsn } : undefined), handler); |
| 124 | + }); |
| 125 | + |
| 126 | + it('accepts a pre-typed options object and spreads of it', () => { |
| 127 | + withSentry(() => preTypedOptions, handler); |
| 128 | + withSentry(() => ({ ...preTypedOptions, dsn }), handler); |
| 129 | + }); |
| 130 | + |
| 131 | + it('still infers the env from the handler', () => { |
| 132 | + withSentry(env => { |
| 133 | + const envDsn: string = env.SENTRY_DSN; |
| 134 | + return { dsn: envDsn }; |
| 135 | + }, handler); |
| 136 | + }); |
| 137 | +}); |
0 commit comments