Skip to content

Commit 41548fb

Browse files
committed
add tests
1 parent 5aeae67 commit 41548fb

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

packages/hono/test/cloudflare/middleware.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,53 @@ describe('Hono Cloudflare Middleware', () => {
116116

117117
expect(middleware.constructor.name).toBe('AsyncFunction');
118118
});
119+
120+
describe('when options is a function (env callback)', () => {
121+
it('calls the options function with the env argument passed by withSentry', () => {
122+
type Bindings = { SENTRY_DSN: string };
123+
const app = new Hono<{ Bindings: Bindings }>();
124+
const mockEnv: Bindings = { SENTRY_DSN: 'https://public@dsn.ingest.sentry.io/1337' };
125+
const optionsFn = vi.fn((env: Bindings) => ({ dsn: env.SENTRY_DSN }));
126+
127+
sentry(app, optionsFn);
128+
129+
const optionsCallback = withSentryMock.mock.calls[0]?.[0];
130+
optionsCallback(mockEnv);
131+
132+
expect(optionsFn).toHaveBeenCalledTimes(1);
133+
expect(optionsFn).toHaveBeenCalledWith(mockEnv);
134+
});
135+
136+
it('uses the return value of the options function as configuration', () => {
137+
type Bindings = { SENTRY_DSN: string };
138+
const app = new Hono<{ Bindings: Bindings }>();
139+
const mockEnv: Bindings = { SENTRY_DSN: 'https://public@dsn.ingest.sentry.io/1337' };
140+
141+
sentry(app, (env: Bindings) => ({ dsn: env.SENTRY_DSN, environment: 'production' }));
142+
143+
const optionsCallback = withSentryMock.mock.calls[0]?.[0];
144+
const result = optionsCallback(mockEnv);
145+
146+
expect(result.dsn).toBe('https://public@dsn.ingest.sentry.io/1337');
147+
expect(result.environment).toBe('production');
148+
});
149+
150+
it('calls applySdkMetadata with the options object returned by the function', () => {
151+
type Bindings = { SENTRY_DSN: string };
152+
const app = new Hono<{ Bindings: Bindings }>();
153+
const mockEnv: Bindings = { SENTRY_DSN: 'https://public@dsn.ingest.sentry.io/1337' };
154+
const returnedOptions = { dsn: 'https://public@dsn.ingest.sentry.io/1337' };
155+
const optionsFn = vi.fn(() => returnedOptions);
156+
157+
sentry(app, optionsFn);
158+
159+
const optionsCallback = withSentryMock.mock.calls[0]?.[0];
160+
optionsCallback(mockEnv);
161+
162+
expect(applySdkMetadataMock).toHaveBeenCalledTimes(1);
163+
expect(applySdkMetadataMock).toHaveBeenCalledWith(returnedOptions, 'hono', ['hono', 'cloudflare']);
164+
});
165+
});
119166
});
120167

121168
describe('filters Hono integration from user-provided integrations', () => {

0 commit comments

Comments
 (0)