-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathwrappers.test.ts
87 lines (67 loc) · 3.17 KB
/
wrappers.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
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
import type { IncomingMessage, ServerResponse } from 'http';
import * as SentryCore from '@sentry/core';
import type { Client } from '@sentry/core';
import { describe, vi, beforeEach, afterEach, test, expect } from 'vitest';
import { wrapGetInitialPropsWithSentry, wrapGetServerSidePropsWithSentry } from '../../src/common';
const startSpanManualSpy = vi.spyOn(SentryCore, 'startSpanManual');
describe('data-fetching function wrappers should not create manual spans', () => {
const route = '/tricks/[trickName]';
let req: IncomingMessage;
let res: ServerResponse;
beforeEach(() => {
req = { headers: {}, url: 'http://dogs.are.great/tricks/kangaroo' } as IncomingMessage;
res = { end: vi.fn() } as unknown as ServerResponse;
vi.spyOn(SentryCore, 'hasSpansEnabled').mockReturnValue(true);
vi.spyOn(SentryCore, 'getClient').mockImplementation(() => {
return {
getOptions: () => ({}),
getDsn: () => {},
} as Client;
});
});
afterEach(() => {
vi.clearAllMocks();
});
test('wrapGetServerSidePropsWithSentry', async () => {
const origFunction = vi.fn(async () => ({ props: {} }));
const wrappedOriginal = wrapGetServerSidePropsWithSentry(origFunction, route);
await wrappedOriginal({ req, res } as any);
expect(startSpanManualSpy).not.toHaveBeenCalled();
});
test('wrapGetInitialPropsWithSentry', async () => {
const origFunction = vi.fn(async () => ({}));
const wrappedOriginal = wrapGetInitialPropsWithSentry(origFunction);
await wrappedOriginal({ req, res, pathname: route } as any);
expect(startSpanManualSpy).not.toHaveBeenCalled();
});
test('wrapped function sets route backfill attribute when called within an active span', async () => {
const mockSetAttribute = vi.fn();
const mockGetActiveSpan = vi.spyOn(SentryCore, 'getActiveSpan').mockReturnValue({
setAttribute: mockSetAttribute,
} as any);
const mockGetRootSpan = vi.spyOn(SentryCore, 'getRootSpan').mockReturnValue({
setAttribute: mockSetAttribute,
} as any);
const origFunction = vi.fn(async () => ({ props: {} }));
const wrappedOriginal = wrapGetServerSidePropsWithSentry(origFunction, route);
await wrappedOriginal({ req, res } as any);
expect(mockGetActiveSpan).toHaveBeenCalled();
expect(mockGetRootSpan).toHaveBeenCalled();
expect(mockSetAttribute).toHaveBeenCalledWith('sentry.route_backfill', '/tricks/[trickName]');
});
test('wrapped function does not set route backfill attribute for /_error route', async () => {
const mockSetAttribute = vi.fn();
const mockGetActiveSpan = vi.spyOn(SentryCore, 'getActiveSpan').mockReturnValue({
setAttribute: mockSetAttribute,
} as any);
const mockGetRootSpan = vi.spyOn(SentryCore, 'getRootSpan').mockReturnValue({
setAttribute: mockSetAttribute,
} as any);
const origFunction = vi.fn(async () => ({ props: {} }));
const wrappedOriginal = wrapGetServerSidePropsWithSentry(origFunction, '/_error');
await wrappedOriginal({ req, res } as any);
expect(mockGetActiveSpan).toHaveBeenCalled();
expect(mockGetRootSpan).not.toHaveBeenCalled();
expect(mockSetAttribute).not.toHaveBeenCalled();
});
});