Skip to content

Commit c562841

Browse files
committed
fix tests
1 parent 0219a5c commit c562841

1 file changed

Lines changed: 1 addition & 137 deletions

File tree

packages/core/test/lib/integrations/express/index.test.ts

Lines changed: 1 addition & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import {
2-
patchExpressModule,
3-
expressErrorHandler,
4-
setupExpressErrorHandler,
5-
} from '../../../../src/integrations/express/index';
1+
import { patchExpressModule } from '../../../../src/integrations/express/index';
62

73
import { describe, it, expect, vi, beforeEach } from 'vitest';
84
import type { Mock } from 'vitest';
@@ -15,39 +11,9 @@ import type {
1511
ExpressRoute,
1612
ExpressRouterv4,
1713
ExpressRouterv5,
18-
ExpressResponse,
19-
ExpressRequest,
20-
ExpressMiddleware,
21-
ExpressErrorMiddleware,
22-
ExpressHandlerOptions,
2314
} from '../../../../src/integrations/express/types';
2415
import type { WrappedFunction } from '../../../../src/types/wrappedfunction';
2516

26-
const sdkProcessingMetadata: unknown[] = [];
27-
const isolationScope = {
28-
_scopeData: {} as { sdkProcessingMetadata?: unknown },
29-
getScopeData() {
30-
return this._scopeData;
31-
},
32-
setSDKProcessingMetadata({ normalizedRequest }: { normalizedRequest: unknown }) {
33-
sdkProcessingMetadata.push(normalizedRequest);
34-
},
35-
};
36-
37-
vi.mock('../../../../src/currentScopes', () => ({
38-
getIsolationScope() {
39-
return isolationScope;
40-
},
41-
}));
42-
43-
const capturedExceptions: [unknown, unknown][] = [];
44-
vi.mock('../../../../src/exports', () => ({
45-
captureException(error: unknown, hint: unknown) {
46-
capturedExceptions.push([error, hint]);
47-
return 'eventId';
48-
},
49-
}));
50-
5117
vi.mock('../../../../src/debug-build', () => ({
5218
DEBUG_BUILD: true,
5319
}));
@@ -261,105 +227,3 @@ describe('patchExpressModule', () => {
261227
]);
262228
});
263229
});
264-
265-
describe('expressErrorHandler', () => {
266-
it('handles the error if it should', () => {
267-
const errorMiddleware = expressErrorHandler();
268-
const res = { status: 500 } as unknown as ExpressResponse;
269-
const next = vi.fn();
270-
const err = new Error('err');
271-
const req = { headers: { request: 'headers' } } as unknown as ExpressRequest;
272-
errorMiddleware(err, req, res, next);
273-
expect((res as unknown as { sentry: string }).sentry).toBe('eventId');
274-
expect(capturedExceptions).toStrictEqual([
275-
[
276-
new Error('err'),
277-
{
278-
mechanism: {
279-
handled: false,
280-
type: 'auto.middleware.express',
281-
},
282-
},
283-
],
284-
]);
285-
capturedExceptions.length = 0;
286-
expect(sdkProcessingMetadata).toStrictEqual([
287-
{
288-
url: undefined,
289-
method: undefined,
290-
query_string: undefined,
291-
headers: Object.assign(Object.create(null), { request: 'headers' }),
292-
cookies: undefined,
293-
data: undefined,
294-
},
295-
]);
296-
sdkProcessingMetadata.length = 0;
297-
expect(next).toHaveBeenCalledExactlyOnceWith(err);
298-
next.mockReset();
299-
});
300-
301-
it('does not the error if it should not', () => {
302-
const errorMiddleware = expressErrorHandler({
303-
shouldHandleError: () => false,
304-
});
305-
const res = { status: 500 } as unknown as ExpressResponse;
306-
const req = { headers: { request: 'headers' } } as unknown as ExpressRequest;
307-
const next = vi.fn();
308-
const err = new Error('err');
309-
errorMiddleware(err, req, res, next);
310-
expect((res as unknown as { sentry?: string }).sentry).toBe(undefined);
311-
expect(capturedExceptions).toStrictEqual([]);
312-
expect(sdkProcessingMetadata).toStrictEqual([
313-
{
314-
url: undefined,
315-
method: undefined,
316-
query_string: undefined,
317-
headers: Object.assign(Object.create(null), { request: 'headers' }),
318-
cookies: undefined,
319-
data: undefined,
320-
},
321-
]);
322-
sdkProcessingMetadata.length = 0;
323-
expect(next).toHaveBeenCalledExactlyOnceWith(err);
324-
next.mockReset();
325-
});
326-
});
327-
328-
describe('setupExpressErrorHandler', () => {
329-
const appUseCalls: unknown[] = [];
330-
const app = {
331-
use: vi.fn((fn: unknown) => appUseCalls.push(fn)) as (
332-
middleware: ExpressMiddleware | ExpressErrorMiddleware,
333-
) => unknown,
334-
};
335-
const options = {} as ExpressHandlerOptions;
336-
it('should have a test here lolz', () => {
337-
setupExpressErrorHandler(app, options);
338-
expect(app.use).toHaveBeenCalledTimes(2);
339-
const reqHandler = appUseCalls[0];
340-
expect(typeof reqHandler).toBe('function');
341-
const next = vi.fn();
342-
(reqHandler as (request: ExpressRequest, _res: ExpressResponse, next: () => void) => void)(
343-
{
344-
method: 'GET',
345-
headers: { request: 'headers' },
346-
} as unknown as ExpressRequest,
347-
{} as unknown as ExpressResponse,
348-
next,
349-
);
350-
expect(next).toHaveBeenCalledOnce();
351-
expect(sdkProcessingMetadata).toStrictEqual([
352-
{
353-
cookies: undefined,
354-
data: undefined,
355-
headers: Object.assign(Object.create(null), {
356-
request: 'headers',
357-
}),
358-
method: 'GET',
359-
query_string: undefined,
360-
url: undefined,
361-
},
362-
]);
363-
sdkProcessingMetadata.length = 0;
364-
});
365-
});

0 commit comments

Comments
 (0)