-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathmiddleware.test.ts
More file actions
205 lines (164 loc) · 7.19 KB
/
middleware.test.ts
File metadata and controls
205 lines (164 loc) · 7.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import * as SentryCloudflare from '@sentry/cloudflare';
import * as SentryCore from '@sentry/core';
import { SDK_VERSION } from '@sentry/core';
import { Hono } from 'hono';
import { beforeEach, describe, expect, it, type Mock, vi } from 'vitest';
import { sentry } from '../../src/cloudflare/middleware';
vi.mock('@sentry/cloudflare', { spy: true });
vi.mock('@sentry/core', async () => {
const actual = await vi.importActual('@sentry/core');
return {
...actual,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
applySdkMetadata: vi.fn(actual.applySdkMetadata),
};
});
const withSentryMock = SentryCloudflare.withSentry as Mock;
const applySdkMetadataMock = SentryCore.applySdkMetadata as Mock;
describe('Hono Cloudflare Middleware', () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe('sentry middleware', () => {
it('calls applySdkMetadata with "hono"', () => {
const app = new Hono();
const options = {
dsn: 'https://public@dsn.ingest.sentry.io/1337',
};
sentry(app, options);
expect(applySdkMetadataMock).toHaveBeenCalledTimes(1);
expect(applySdkMetadataMock).toHaveBeenCalledWith(options, 'hono');
});
it('calls withSentry with modified options', () => {
const app = new Hono();
const options = {
dsn: 'https://public@dsn.ingest.sentry.io/1337',
};
sentry(app, options);
expect(withSentryMock).toHaveBeenCalledTimes(1);
expect(withSentryMock).toHaveBeenCalledWith(expect.any(Function), app);
// Get the options callback and call it
const optionsCallback = withSentryMock.mock.calls[0]?.[0];
expect(optionsCallback).toBeInstanceOf(Function);
const result = optionsCallback();
// After applySdkMetadata is called, options should have _metadata.sdk
expect(result.dsn).toBe('https://public@dsn.ingest.sentry.io/1337');
expect(result._metadata?.sdk?.name).toBe('sentry.javascript.hono');
expect(result._metadata?.sdk?.version).toBe(SDK_VERSION);
expect(result._metadata?.sdk?.packages).toEqual([
{
name: 'npm:@sentry/hono',
version: SDK_VERSION,
},
]);
});
it('calls applySdkMetadata before withSentry', () => {
const app = new Hono();
const options = {
dsn: 'https://public@dsn.ingest.sentry.io/1337',
};
sentry(app, options);
// Verify applySdkMetadata was called before withSentry
const applySdkMetadataCallOrder = applySdkMetadataMock.mock.invocationCallOrder[0];
const withSentryCallOrder = withSentryMock.mock.invocationCallOrder[0];
expect(applySdkMetadataCallOrder).toBeLessThan(withSentryCallOrder as number);
});
it('preserves all user options', () => {
const app = new Hono();
const options = {
dsn: 'https://public@dsn.ingest.sentry.io/1337',
environment: 'production',
sampleRate: 0.5,
tracesSampleRate: 1.0,
debug: true,
};
sentry(app, options);
const optionsCallback = withSentryMock.mock.calls[0]?.[0];
const result = optionsCallback();
expect(result).toMatchObject({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
environment: 'production',
sampleRate: 0.5,
tracesSampleRate: 1.0,
debug: true,
});
});
it('returns a middleware handler function', () => {
const app = new Hono();
const options = {
dsn: 'https://public@dsn.ingest.sentry.io/1337',
};
const middleware = sentry(app, options);
expect(middleware).toBeDefined();
expect(typeof middleware).toBe('function');
expect(middleware).toHaveLength(2); // Hono middleware takes (context, next)
});
it('returns an async middleware handler', async () => {
const app = new Hono();
const middleware = sentry(app, {});
expect(middleware.constructor.name).toBe('AsyncFunction');
});
});
describe('filters Hono integration from user-provided integrations', () => {
const honoIntegration = { name: 'Hono' } as SentryCore.Integration;
const otherIntegration = { name: 'Other' } as SentryCore.Integration;
const getIntegrationsResult = () => {
const optionsCallback = withSentryMock.mock.calls[0]?.[0];
return optionsCallback().integrations;
};
it.each([
['filters Hono integration out', [honoIntegration, otherIntegration], [otherIntegration]],
['keeps non-Hono integrations', [otherIntegration], [otherIntegration]],
['returns empty array when only Hono integration provided', [honoIntegration], []],
])('%s (array)', (_name, input, expected) => {
const app = new Hono();
sentry(app, { integrations: input });
const integrationsFn = getIntegrationsResult() as (defaults: SentryCore.Integration[]) => SentryCore.Integration[];
expect(integrationsFn([])).toEqual(expected);
});
it('filters Hono from defaults when user provides an array', () => {
const app = new Hono();
sentry(app, { integrations: [otherIntegration] });
const integrationsFn = getIntegrationsResult() as (defaults: SentryCore.Integration[]) => SentryCore.Integration[];
// Simulates getIntegrationsToSetup: defaults (from Cloudflare) include Hono; result must exclude it
const defaultsWithHono = [honoIntegration, otherIntegration];
expect(integrationsFn(defaultsWithHono)).toEqual([otherIntegration, otherIntegration]);
});
it('filters Hono integration out of a function result', () => {
const app = new Hono();
sentry(app, { integrations: () => [honoIntegration, otherIntegration] });
const integrationsFn = getIntegrationsResult() as unknown as (
defaults: SentryCore.Integration[],
) => SentryCore.Integration[];
expect(integrationsFn([])).toEqual([otherIntegration]);
});
it('passes defaults through to the user-provided integrations function', () => {
const app = new Hono();
const userFn = vi.fn((_defaults: SentryCore.Integration[]) => [otherIntegration]);
const defaults = [{ name: 'Default' } as SentryCore.Integration];
sentry(app, { integrations: userFn });
const integrationsFn = getIntegrationsResult() as unknown as (
defaults: SentryCore.Integration[],
) => SentryCore.Integration[];
integrationsFn(defaults);
expect(userFn).toHaveBeenCalledWith(defaults);
});
it('filters Hono integration returned by the user-provided integrations function', () => {
const app = new Hono();
sentry(app, { integrations: (_defaults: SentryCore.Integration[]) => [honoIntegration] });
const integrationsFn = getIntegrationsResult() as unknown as (
defaults: SentryCore.Integration[],
) => SentryCore.Integration[];
expect(integrationsFn([])).toEqual([]);
});
it('filters Hono integration from defaults when integrations is undefined', () => {
const app = new Hono();
sentry(app, {});
const integrationsFn = getIntegrationsResult() as unknown as (
defaults: SentryCore.Integration[],
) => SentryCore.Integration[];
expect(integrationsFn([honoIntegration, otherIntegration])).toEqual([otherIntegration]);
});
});
});