-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathwithSentry.test.ts
55 lines (47 loc) · 1.81 KB
/
withSentry.test.ts
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
import * as SentryCore from '@sentry/core';
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core';
import type { NextApiRequest, NextApiResponse } from 'next';
import { describe, vi, beforeEach, afterEach, it, expect } from 'vitest';
import type { AugmentedNextApiResponse, NextApiHandler } from '../../src/common/types';
import { wrapApiHandlerWithSentry } from '../../src/server';
const startSpanManualSpy = vi.spyOn(SentryCore, 'startSpanManual');
describe('withSentry', () => {
let req: NextApiRequest, res: NextApiResponse;
const origHandlerNoError: NextApiHandler = async (_req, res) => {
res.send('Good dog, Maisey!');
};
const wrappedHandlerNoError = wrapApiHandlerWithSentry(origHandlerNoError, '/my-parameterized-route');
beforeEach(() => {
req = { url: 'http://dogs.are.great' } as NextApiRequest;
res = {
send: function (this: AugmentedNextApiResponse) {
this.end();
},
end: function (this: AugmentedNextApiResponse) {
// eslint-disable-next-line deprecation/deprecation
this.finished = true;
// @ts-expect-error This is a mock
this.writableEnded = true;
},
} as unknown as AugmentedNextApiResponse;
});
afterEach(() => {
vi.clearAllMocks();
});
describe('tracing', () => {
it('starts a transaction when tracing is enabled', async () => {
await wrappedHandlerNoError(req, res);
expect(startSpanManualSpy).toHaveBeenCalledWith(
expect.objectContaining({
name: 'GET /my-parameterized-route',
op: 'http.server',
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.nextjs',
},
}),
expect.any(Function),
);
});
});
});