-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathpatchAppUse.test.ts
More file actions
158 lines (126 loc) · 4.93 KB
/
patchAppUse.test.ts
File metadata and controls
158 lines (126 loc) · 4.93 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
import * as SentryCore from '@sentry/core';
import { Hono } from 'hono';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { patchAppUse } from '../../src/shared/patchAppUse';
vi.mock('@sentry/core', async () => {
const actual = await vi.importActual('@sentry/core');
return {
...actual,
startInactiveSpan: vi.fn((_opts: unknown) => ({
setStatus: vi.fn(),
end: vi.fn(),
})),
captureException: vi.fn(),
};
});
const startInactiveSpanMock = SentryCore.startInactiveSpan as ReturnType<typeof vi.fn>;
const captureExceptionMock = SentryCore.captureException as ReturnType<typeof vi.fn>;
describe('patchAppUse (middleware spans)', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('wraps handlers in app.use(handler) so startInactiveSpan is called when middleware runs', async () => {
const app = new Hono();
patchAppUse(app);
const userHandler = vi.fn(async (_c: unknown, next: () => Promise<void>) => {
await next();
});
app.use(userHandler);
expect(startInactiveSpanMock).not.toHaveBeenCalled();
const fetchHandler = app.fetch;
const req = new Request('http://localhost/');
await fetchHandler(req);
expect(startInactiveSpanMock).toHaveBeenCalledTimes(1);
expect(startInactiveSpanMock).toHaveBeenCalledWith(
expect.objectContaining({
op: 'middleware.hono',
onlyIfParent: true,
attributes: expect.objectContaining({
'sentry.op': 'middleware.hono',
'sentry.origin': 'auto.middleware.hono',
}),
}),
);
expect(userHandler).toHaveBeenCalled();
});
describe('span naming', () => {
it('uses handler.name for span when handler has a name', async () => {
const app = new Hono();
patchAppUse(app);
async function myNamedMiddleware(_c: unknown, next: () => Promise<void>) {
await next();
}
app.use(myNamedMiddleware);
await app.fetch(new Request('http://localhost/'));
expect(startInactiveSpanMock).toHaveBeenCalledWith(expect.objectContaining({ name: 'myNamedMiddleware' }));
});
it('uses <anonymous.index> for span when handler is anonymous', async () => {
const app = new Hono();
patchAppUse(app);
app.use(async (_c: unknown, next: () => Promise<void>) => next());
await app.fetch(new Request('http://localhost/'));
expect(startInactiveSpanMock).toHaveBeenCalledTimes(1);
const name = startInactiveSpanMock.mock.calls[0][0].name;
expect(name).toMatch('<anonymous>');
});
});
it('wraps each handler in app.use(path, ...handlers) and passes path through', async () => {
const app = new Hono();
patchAppUse(app);
const handler = async (_c: unknown, next: () => Promise<void>) => next();
app.use('/api', handler);
app.get('/api', () => new Response('ok'));
await app.fetch(new Request('http://localhost/api'));
expect(startInactiveSpanMock).toHaveBeenCalled();
});
it('calls captureException when middleware throws', async () => {
const app = new Hono();
patchAppUse(app);
const err = new Error('middleware error');
app.use(async () => {
throw err;
});
const res = await app.fetch(new Request('http://localhost/'));
expect(res.status).toBe(500);
expect(captureExceptionMock).toHaveBeenCalledWith(err, {
mechanism: { handled: false, type: 'auto.middleware.hono' },
});
});
it('creates sibling spans for multiple middlewares (onion order, not parent-child)', async () => {
const app = new Hono();
patchAppUse(app);
app.use(
async (_c: unknown, next: () => Promise<void>) => next(),
async function namedMiddleware(_c: unknown, next: () => Promise<void>) {
await next();
},
async (_c: unknown, next: () => Promise<void>) => next(),
);
await app.fetch(new Request('http://localhost/'));
expect(startInactiveSpanMock).toHaveBeenCalledTimes(3);
const [firstCall, secondCall, thirdCall] = startInactiveSpanMock.mock.calls;
expect(firstCall[0]).toMatchObject({ op: 'middleware.hono' });
expect(secondCall[0]).toMatchObject({ op: 'middleware.hono' });
expect(firstCall[0].name).toMatch('<anonymous>');
expect(secondCall[0].name).toBe('namedMiddleware');
expect(thirdCall[0].name).toBe('<anonymous>');
expect(firstCall[0].name).not.toBe(secondCall[0].name);
});
it('preserves this context when calling the original use (Proxy forwards thisArg)', () => {
type FakeApp = {
_capturedThis: unknown;
use: (...args: unknown[]) => FakeApp;
};
const fakeApp: FakeApp = {
_capturedThis: null,
use(this: FakeApp, ..._args: unknown[]) {
this._capturedThis = this;
return this;
},
};
patchAppUse(fakeApp as unknown as Parameters<typeof patchAppUse>[0]);
const noop = async (_c: unknown, next: () => Promise<void>) => next();
fakeApp.use(noop);
expect(fakeApp._capturedThis).toBe(fakeApp);
});
});