-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathindex.client.test.ts
162 lines (135 loc) · 6 KB
/
index.client.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
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
159
160
161
162
import { BaseClient } from '@sentry/core';
import { getCurrentHub } from '@sentry/hub';
import * as SentryReact from '@sentry/react';
import { Integrations as TracingIntegrations } from '@sentry/tracing';
import { Integration } from '@sentry/types';
import { getGlobalObject, logger, SentryError } from '@sentry/utils';
import { init, Integrations, nextRouterInstrumentation } from '../src/index.client';
import { NextjsOptions } from '../src/utils/nextjsOptions';
const { BrowserTracing } = TracingIntegrations;
const global = getGlobalObject();
const reactInit = jest.spyOn(SentryReact, 'init');
const captureEvent = jest.spyOn(BaseClient.prototype, 'captureEvent');
const logError = jest.spyOn(logger, 'error');
describe('Client init()', () => {
afterEach(() => {
jest.clearAllMocks();
global.__SENTRY__.hub = undefined;
});
it('inits the React SDK', () => {
expect(reactInit).toHaveBeenCalledTimes(0);
init({});
expect(reactInit).toHaveBeenCalledTimes(1);
expect(reactInit).toHaveBeenCalledWith(
expect.objectContaining({
_metadata: {
sdk: {
name: 'sentry.javascript.nextjs',
version: expect.any(String),
packages: [
{
name: 'npm:@sentry/nextjs',
version: expect.any(String),
},
{
name: 'npm:@sentry/react',
version: expect.any(String),
},
],
},
},
environment: 'test',
integrations: undefined,
}),
);
});
it('sets runtime on scope', () => {
const currentScope = getCurrentHub().getScope();
// @ts-ignore need access to protected _tags attribute
expect(currentScope._tags).toEqual({});
init({});
// @ts-ignore need access to protected _tags attribute
expect(currentScope._tags).toEqual({ runtime: 'browser' });
});
it('adds 404 transaction filter', () => {
init({
dsn: 'https://[email protected]/12312012',
tracesSampleRate: 1.0,
});
const hub = getCurrentHub();
const sendEvent = jest.spyOn(hub.getClient()!.getTransport!(), 'sendEvent');
const transaction = hub.startTransaction({ name: '/404' });
transaction.finish();
expect(sendEvent).not.toHaveBeenCalled();
expect(captureEvent.mock.results[0].value).toBeUndefined();
expect(logError).toHaveBeenCalledWith(new SentryError('An event processor returned null, will not send event.'));
});
describe('integrations', () => {
it('does not add BrowserTracing integration by default if tracesSampleRate is not set', () => {
init({});
const reactInitOptions: NextjsOptions = reactInit.mock.calls[0][0];
expect(reactInitOptions.integrations).toBeUndefined();
});
it('adds BrowserTracing integration by default if tracesSampleRate is set', () => {
init({ tracesSampleRate: 1.0 });
const reactInitOptions: NextjsOptions = reactInit.mock.calls[0][0];
expect(reactInitOptions.integrations).toHaveLength(1);
const integrations = reactInitOptions.integrations as Integration[];
expect(integrations[0]).toEqual(expect.any(BrowserTracing));
// eslint-disable-next-line @typescript-eslint/unbound-method
expect((integrations[0] as InstanceType<typeof BrowserTracing>).options.routingInstrumentation).toEqual(
nextRouterInstrumentation,
);
});
it('adds BrowserTracing integration by default if tracesSampler is set', () => {
init({ tracesSampler: () => true });
const reactInitOptions: NextjsOptions = reactInit.mock.calls[0][0];
expect(reactInitOptions.integrations).toHaveLength(1);
const integrations = reactInitOptions.integrations as Integration[];
expect(integrations[0]).toEqual(expect.any(BrowserTracing));
// eslint-disable-next-line @typescript-eslint/unbound-method
expect((integrations[0] as InstanceType<typeof BrowserTracing>).options.routingInstrumentation).toEqual(
nextRouterInstrumentation,
);
});
it('supports passing integration through options', () => {
init({ tracesSampleRate: 1.0, integrations: [new Integrations.Breadcrumbs({ console: false })] });
const reactInitOptions: NextjsOptions = reactInit.mock.calls[0][0];
expect(reactInitOptions.integrations).toHaveLength(2);
const integrations = reactInitOptions.integrations as Integration[];
expect(integrations).toEqual([expect.any(Integrations.Breadcrumbs), expect.any(BrowserTracing)]);
});
it('uses custom BrowserTracing with array option with nextRouterInstrumentation', () => {
init({
tracesSampleRate: 1.0,
integrations: [new BrowserTracing({ idleTimeout: 5000, startTransactionOnLocationChange: false })],
});
const reactInitOptions: NextjsOptions = reactInit.mock.calls[0][0];
expect(reactInitOptions.integrations).toHaveLength(1);
const integrations = reactInitOptions.integrations as Integration[];
expect((integrations[0] as InstanceType<typeof BrowserTracing>).options).toEqual(
expect.objectContaining({
idleTimeout: 5000,
startTransactionOnLocationChange: false,
routingInstrumentation: nextRouterInstrumentation,
}),
);
});
it('uses custom BrowserTracing with function option with nextRouterInstrumentation', () => {
init({
tracesSampleRate: 1.0,
integrations: () => [new BrowserTracing({ idleTimeout: 5000, startTransactionOnLocationChange: false })],
});
const reactInitOptions: NextjsOptions = reactInit.mock.calls[0][0];
const integrationFunc = reactInitOptions.integrations as () => Integration[];
const integrations = integrationFunc();
expect((integrations[0] as InstanceType<typeof BrowserTracing>).options).toEqual(
expect.objectContaining({
idleTimeout: 5000,
startTransactionOnLocationChange: false,
routingInstrumentation: nextRouterInstrumentation,
}),
);
});
});
});