-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathwithSentry.test.ts
More file actions
103 lines (81 loc) · 3.46 KB
/
withSentry.test.ts
File metadata and controls
103 lines (81 loc) · 3.46 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
// Note: These tests run the handler in Node.js, which has some differences to the cloudflare workers runtime.
// Although this is not ideal, this is the best we can do until we have a better way to test cloudflare workers.
import * as SentryCore from '@sentry/core';
import { beforeEach, describe, expect, test, vi } from 'vitest';
import { withSentry } from '../src/withSentry';
import { markAsInstrumented } from '../src/instrument';
import * as HonoIntegration from '../src/integrations/hono';
type HonoLikeApp<Env = unknown, QueueHandlerMessage = unknown, CfHostMetadata = unknown> = ExportedHandler<
Env,
QueueHandlerMessage,
CfHostMetadata
> & {
onError?: () => void;
errorHandler?: (err: Error) => Response;
};
const MOCK_ENV = {
SENTRY_DSN: 'https://public@dsn.ingest.sentry.io/1337',
SENTRY_RELEASE: '1.1.1',
};
describe('withSentry', () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe('hono errorHandler', () => {
test('calls Hono Integration to handle error captured by the errorHandler', async () => {
const error = new Error('test hono error');
const handleHonoException = vi.fn();
vi.spyOn(HonoIntegration, 'getHonoIntegration').mockReturnValue({ handleHonoException } as any);
const honoApp = {
fetch(_request, _env, _context) {
return new Response('test');
},
onError() {},
errorHandler(err: Error) {
return new Response(`Error: ${err.message}`, { status: 500 });
},
} satisfies HonoLikeApp<typeof MOCK_ENV>;
withSentry(env => ({ dsn: env.SENTRY_DSN }), honoApp);
const errorHandlerResponse = honoApp.errorHandler?.(error);
expect(handleHonoException).toHaveBeenCalledTimes(1);
expect(handleHonoException).toHaveBeenLastCalledWith(error, undefined);
expect(errorHandlerResponse?.status).toBe(500);
});
test('preserves the original errorHandler functionality', async () => {
const originalErrorHandlerSpy = vi.fn().mockImplementation((err: Error) => {
return new Response(`Error: ${err.message}`, { status: 500 });
});
const error = new Error('test hono error');
const honoApp = {
fetch(_request, _env, _context) {
return new Response('test');
},
onError() {},
errorHandler: originalErrorHandlerSpy,
} satisfies HonoLikeApp<typeof MOCK_ENV>;
withSentry(env => ({ dsn: env.SENTRY_DSN }), honoApp);
const errorHandlerResponse = honoApp.errorHandler?.(error);
expect(originalErrorHandlerSpy).toHaveBeenCalledTimes(1);
expect(originalErrorHandlerSpy).toHaveBeenLastCalledWith(error);
expect(errorHandlerResponse?.status).toBe(500);
});
test('does not instrument an already instrumented errorHandler', async () => {
const captureExceptionSpy = vi.spyOn(SentryCore, 'captureException');
const error = new Error('test hono error');
const originalErrorHandler = (err: Error) => {
return new Response(`Error: ${err.message}`, { status: 500 });
};
markAsInstrumented(originalErrorHandler);
const honoApp = {
fetch(_request, _env, _context) {
return new Response('test');
},
onError() {},
errorHandler: originalErrorHandler,
} satisfies HonoLikeApp<typeof MOCK_ENV>;
withSentry(env => ({ dsn: env.SENTRY_DSN }), honoApp);
honoApp.errorHandler?.(error);
expect(captureExceptionSpy).not.toHaveBeenCalled();
});
});
});