@servicetitan/datadog-rum 40.2.2 → 40.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/dist/create-impl.d.ts.map +1 -1
  2. package/dist/create-impl.js +3 -0
  3. package/dist/create-impl.js.map +1 -1
  4. package/dist/datadog-provider.d.ts +3 -1
  5. package/dist/datadog-provider.d.ts.map +1 -1
  6. package/dist/datadog-provider.js +3 -1
  7. package/dist/datadog-provider.js.map +1 -1
  8. package/dist/get-env.d.ts +7 -0
  9. package/dist/get-env.d.ts.map +1 -0
  10. package/dist/get-env.js +26 -0
  11. package/dist/get-env.js.map +1 -0
  12. package/dist/hash-routing.d.ts +5 -1
  13. package/dist/hash-routing.d.ts.map +1 -1
  14. package/dist/hash-routing.js +23 -8
  15. package/dist/hash-routing.js.map +1 -1
  16. package/dist/index.d.ts +1 -0
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +1 -0
  19. package/dist/index.js.map +1 -1
  20. package/dist/sanitize-view-name.d.ts.map +1 -1
  21. package/dist/sanitize-view-name.js +10 -0
  22. package/dist/sanitize-view-name.js.map +1 -1
  23. package/dist/shared-impl.d.ts +1 -1
  24. package/dist/shared-impl.d.ts.map +1 -1
  25. package/dist/shared-impl.js.map +1 -1
  26. package/dist/types.d.ts +1 -2
  27. package/dist/types.d.ts.map +1 -1
  28. package/dist/types.js.map +1 -1
  29. package/package.json +8 -3
  30. package/src/create-impl.ts +2 -0
  31. package/src/datadog-provider.tsx +7 -3
  32. package/src/get-env.ts +27 -0
  33. package/src/hash-routing.tsx +28 -9
  34. package/src/index.ts +1 -0
  35. package/src/sanitize-view-name.tsx +17 -0
  36. package/src/shared-impl.ts +1 -1
  37. package/src/types.ts +6 -2
  38. package/src/__mocks__/before-send-data.ts +0 -538
  39. package/src/__mocks__/index.ts +0 -7
  40. package/src/__mocks__/init-config.ts +0 -6
  41. package/src/__mocks__/mfe-host.ts +0 -7
  42. package/src/__mocks__/process-event.ts +0 -20
  43. package/src/__mocks__/reset-shared-impl.ts +0 -8
  44. package/src/__mocks__/tracing-urls.ts +0 -30
  45. package/src/__mocks__/types.ts +0 -1
  46. package/src/__tests__/before-send.test.ts +0 -76
  47. package/src/__tests__/build-allowed-tracing-urls.test.ts +0 -120
  48. package/src/__tests__/build-before-send.test.ts +0 -193
  49. package/src/__tests__/create-impl.test.ts +0 -427
  50. package/src/__tests__/datadog-provider.test.tsx +0 -145
  51. package/src/__tests__/dd-rum-guard.test.ts +0 -73
  52. package/src/__tests__/element-service-recorder.test.ts +0 -127
  53. package/src/__tests__/enrich-event.test.ts +0 -139
  54. package/src/__tests__/get-shared.test.ts +0 -56
  55. package/src/__tests__/handle-resource.test.ts +0 -76
  56. package/src/__tests__/hash-routing.test.tsx +0 -70
  57. package/src/__tests__/init.test.ts +0 -69
  58. package/src/__tests__/integration.test.ts +0 -147
  59. package/src/__tests__/resolve-path-service.test.ts +0 -130
  60. package/src/__tests__/sanitize-view-name.test.tsx +0 -92
  61. package/src/__tests__/set-user.test.ts +0 -61
  62. package/src/__tests__/use-rum-service.test.tsx +0 -82
  63. package/src/runtime-override/__tests__/dev-console.test.ts +0 -158
  64. package/src/runtime-override/__tests__/override.test.ts +0 -109
@@ -1,427 +0,0 @@
1
- import { datadogRum } from '@datadog/browser-rum';
2
- import { minimalApplication } from '../__mocks__';
3
- import { buildAllowedTracingUrls } from '../build-allowed-tracing-urls';
4
- import { buildBeforeSend } from '../build-before-send';
5
- import { createImpl } from '../create-impl';
6
- import { installDdRumGuard } from '../dd-rum-guard';
7
- import { elementServiceRecorder } from '../element-service-recorder';
8
- import { DATADOG_RUM_FOR_DEV, installDevConsole, isForceEnabled } from '../runtime-override';
9
- import type { SharedImpl } from '../shared-impl';
10
- import type { ApplicationConfig, InitRumOptions, State } from '../types';
11
-
12
- jest.mock('@datadog/browser-rum');
13
- jest.mock('../build-before-send');
14
- jest.mock('../build-allowed-tracing-urls');
15
- jest.mock('../dd-rum-guard');
16
- jest.mock('../element-service-recorder');
17
- jest.mock('../runtime-override', () => ({
18
- ...jest.requireActual('../runtime-override'),
19
- isForceEnabled: jest.fn(),
20
- installDevConsole: jest.fn(),
21
- }));
22
-
23
- describe(`[datadog-rum] ${createImpl.name}`, () => {
24
- const mockBeforeSend = jest.fn();
25
- const mockAllowedTracingUrls = jest.fn();
26
- let impl: SharedImpl;
27
-
28
- beforeEach(() => {
29
- jest.clearAllMocks();
30
- jest.mocked(datadogRum.getInitConfiguration).mockReturnValue(undefined);
31
- jest.mocked(isForceEnabled).mockReturnValue(false);
32
- jest.mocked(buildBeforeSend).mockReturnValue(mockBeforeSend);
33
- jest.mocked(buildAllowedTracingUrls).mockReturnValue(mockAllowedTracingUrls as any);
34
- impl = createImpl();
35
- });
36
-
37
- /*
38
- * createImpl passes the same live state object to both collaborators, so the
39
- * captured argument reflects later mutations from init/setServiceContext/cleanup.
40
- */
41
- const capturedState = () => jest.mocked(buildBeforeSend).mock.calls[0][0] as State;
42
-
43
- describe('init', () => {
44
- let options: InitRumOptions;
45
-
46
- beforeEach(() => {
47
- options = { service: { name: 'desktop' }, application: { ...minimalApplication } };
48
- });
49
-
50
- const subject = () => impl.init(options);
51
-
52
- test('calls datadogRum.init', () => {
53
- subject();
54
-
55
- expect(datadogRum.init).toHaveBeenCalledWith(
56
- expect.objectContaining({
57
- ...options.application,
58
- sessionSampleRate: 100,
59
- sessionReplaySampleRate: 0,
60
- defaultPrivacyLevel: 'mask-user-input',
61
- service: options.service.name,
62
- beforeSend: mockBeforeSend,
63
- allowedTracingUrls: mockAllowedTracingUrls,
64
- })
65
- );
66
- });
67
-
68
- test('reports it initialized the SDK', () => {
69
- expect(subject().initializedSdk).toBe(true);
70
- });
71
-
72
- test('registers the service', () => {
73
- subject();
74
-
75
- expect(capturedState().services.has(options.service.name)).toBe(true);
76
- });
77
-
78
- itDoesNotInstallDevConsole();
79
-
80
- itDoesNotTagTheSession();
81
-
82
- describe('with a stale override present', () => {
83
- beforeEach(() => {
84
- jest.mocked(isForceEnabled).mockReturnValue(true);
85
- });
86
-
87
- itDoesNotTagTheSession();
88
- });
89
-
90
- describe('when config overrides defaults', () => {
91
- const overrides = {
92
- sessionSampleRate: 25,
93
- sessionReplaySampleRate: 10,
94
- defaultPrivacyLevel: 'allow',
95
- } as const;
96
-
97
- beforeEach(() => {
98
- options.application = { ...minimalApplication, ...overrides };
99
- });
100
-
101
- test('calls datadogRum.init with overridden values', () => {
102
- subject();
103
-
104
- expect(datadogRum.init).toHaveBeenCalledWith(expect.objectContaining(overrides));
105
- });
106
- });
107
-
108
- describe('when config contains extra values', () => {
109
- const excludedActivityUrls = [/pendo\.io/];
110
-
111
- beforeEach(() => {
112
- options.application = { ...minimalApplication, excludedActivityUrls };
113
- });
114
-
115
- test('calls datadogRum.init with extra values', () => {
116
- subject();
117
-
118
- expect(datadogRum.init).toHaveBeenCalledWith(
119
- expect.objectContaining({ excludedActivityUrls })
120
- );
121
- });
122
- });
123
-
124
- describe('with an application beforeSend handler', () => {
125
- const appBeforeSend = jest.fn();
126
-
127
- beforeEach(() => {
128
- options.application = { ...minimalApplication, beforeSend: appBeforeSend };
129
- });
130
-
131
- test('registers the handler', () => {
132
- subject();
133
-
134
- expect(capturedState().applicationBeforeSend).toBe(appBeforeSend);
135
- });
136
- });
137
-
138
- function itDoesNotCallDatadogRumInit() {
139
- test('does not call datadogRum.init', () => {
140
- subject();
141
-
142
- expect(datadogRum.init).not.toHaveBeenCalled();
143
- });
144
-
145
- test('reports it did not initialize the SDK', () => {
146
- expect(subject().initializedSdk).toBe(false);
147
- });
148
- }
149
-
150
- function itDoesNotInstallDevConsole() {
151
- test('does not install the dev console handle', () => {
152
- subject();
153
-
154
- expect(installDevConsole).not.toHaveBeenCalled();
155
- });
156
- }
157
-
158
- function itDoesNotTagTheSession() {
159
- test('does not tag the session', () => {
160
- subject();
161
-
162
- expect(datadogRum.setGlobalContextProperty).not.toHaveBeenCalled();
163
- });
164
- }
165
-
166
- test('installs the window.DD_RUM guard', () => {
167
- subject();
168
-
169
- expect(installDdRumGuard).toHaveBeenCalledWith(datadogRum);
170
- });
171
-
172
- test('installs the element service recorder', () => {
173
- subject();
174
-
175
- expect(elementServiceRecorder.install).toHaveBeenCalled();
176
- });
177
-
178
- describe('when already called', () => {
179
- beforeEach(() => {
180
- subject();
181
- jest.clearAllMocks();
182
- jest.spyOn(console, 'info').mockImplementation(() => {});
183
- });
184
-
185
- itDoesNotCallDatadogRumInit();
186
-
187
- test('logs an info message', () => {
188
- const infoSpy = jest.spyOn(console, 'info');
189
-
190
- subject();
191
-
192
- expect(infoSpy).toHaveBeenCalledWith(
193
- expect.stringContaining('the SDK is already started')
194
- );
195
- });
196
- });
197
-
198
- describe('when a session is already started', () => {
199
- beforeEach(() => subject());
200
-
201
- describe('when an application is offered', () => {
202
- beforeEach(() => (options.enabled = false));
203
-
204
- test('does not initialize a second time', () => {
205
- subject();
206
-
207
- expect(datadogRum.init).toHaveBeenCalledTimes(1);
208
- });
209
-
210
- test('does not start the late offer afterwards', () => {
211
- subject();
212
- impl.init({ service: { name: 'task-hub-my-tasks' } });
213
-
214
- expect(datadogRum.init).toHaveBeenCalledTimes(1);
215
- });
216
- });
217
- });
218
-
219
- describe('when datadogRum.init was already called directly', () => {
220
- beforeEach(() => {
221
- jest.spyOn(console, 'warn').mockImplementation(() => {});
222
- jest.mocked(datadogRum.getInitConfiguration).mockReturnValue({} as any);
223
- });
224
-
225
- itDoesNotCallDatadogRumInit();
226
-
227
- test('warns the SDK was initialized outside the facade', () => {
228
- const warnSpy = jest.spyOn(console, 'warn');
229
-
230
- subject();
231
-
232
- expect(warnSpy).toHaveBeenCalledWith(
233
- expect.stringContaining('SDK was initialized outside the facade')
234
- );
235
- });
236
- });
237
-
238
- describe('with service only (no application)', () => {
239
- beforeEach(() => {
240
- delete options.application;
241
- });
242
-
243
- itDoesNotCallDatadogRumInit();
244
- });
245
-
246
- describe('when enabled is false', () => {
247
- beforeEach(() => {
248
- options.enabled = false;
249
- });
250
-
251
- test('installs the dev console handle', () => {
252
- subject();
253
-
254
- expect(installDevConsole).toHaveBeenCalled();
255
- });
256
-
257
- itDoesNotCallDatadogRumInit();
258
-
259
- describe('with an active override', () => {
260
- beforeEach(() => {
261
- jest.mocked(isForceEnabled).mockReturnValue(true);
262
- });
263
-
264
- test('initializes the SDK', () => {
265
- subject();
266
-
267
- expect(datadogRum.init).toHaveBeenCalled();
268
- });
269
-
270
- test('reports it initialized the SDK', () => {
271
- expect(subject().initializedSdk).toBe(true);
272
- });
273
-
274
- test('tags the session as override-driven', () => {
275
- subject();
276
-
277
- expect(datadogRum.setGlobalContextProperty).toHaveBeenCalledWith(
278
- DATADOG_RUM_FOR_DEV,
279
- true
280
- );
281
- });
282
- });
283
-
284
- describe('with no application', () => {
285
- beforeEach(() => {
286
- delete options.application;
287
- });
288
-
289
- itDoesNotInstallDevConsole();
290
- });
291
-
292
- describe('when the application is offered', () => {
293
- const host = 'desktop';
294
- const guest = 'task-hub-my-tasks';
295
- const askForSession = (application?: ApplicationConfig) =>
296
- impl.init({ service: { name: guest }, application });
297
-
298
- const expectOfferedApplication = () =>
299
- expect(datadogRum.init).toHaveBeenCalledWith(
300
- expect.objectContaining({
301
- applicationId: minimalApplication.applicationId,
302
- service: host,
303
- })
304
- );
305
-
306
- beforeEach(() => {
307
- jest.spyOn(console, 'info').mockImplementation(() => {});
308
- subject();
309
- });
310
-
311
- describe('when a later caller asks for a session', () => {
312
- const guestApplication = {
313
- ...minimalApplication,
314
- applicationId: 'guest-app-id',
315
- };
316
-
317
- beforeEach(() => askForSession(guestApplication));
318
-
319
- test('initializes with the offered application', expectOfferedApplication);
320
-
321
- test("does not initialize with the later caller's application", () => {
322
- expect(datadogRum.init).not.toHaveBeenCalledWith(
323
- expect.objectContaining({ applicationId: 'guest-app-id' })
324
- );
325
- });
326
-
327
- test('records the service that started the session', () => {
328
- expect(datadogRum.setGlobalContextProperty).toHaveBeenCalledWith(
329
- 'initializedBy',
330
- { service: guest }
331
- );
332
- });
333
-
334
- test('does not tag the session as a dev override', () => {
335
- expect(datadogRum.setGlobalContextProperty).not.toHaveBeenCalledWith(
336
- DATADOG_RUM_FOR_DEV,
337
- true
338
- );
339
- });
340
-
341
- test("registers the later caller's service", () => {
342
- expect(capturedState().services.has(guest)).toBe(true);
343
- });
344
-
345
- test('logs which application started', () => {
346
- expect(jest.spyOn(console, 'info')).toHaveBeenCalledWith(
347
- expect.stringContaining(`'${guest}' ignored`)
348
- );
349
- });
350
- });
351
-
352
- describe('when a later caller asks with no application', () => {
353
- beforeEach(() => askForSession());
354
-
355
- test('initializes with the offered application', expectOfferedApplication);
356
-
357
- test('records the service that started the session', () => {
358
- expect(datadogRum.setGlobalContextProperty).toHaveBeenCalledWith(
359
- 'initializedBy',
360
- { service: guest }
361
- );
362
- });
363
-
364
- test('does not report an ignored application', () => {
365
- expect(jest.spyOn(console, 'info')).not.toHaveBeenCalled();
366
- });
367
- });
368
-
369
- describe('when a later caller offers another application', () => {
370
- beforeEach(() => {
371
- impl.init({
372
- service: { name: 'enterprise-hub' },
373
- application: { ...minimalApplication, applicationId: 'later-app-id' },
374
- enabled: false,
375
- });
376
- askForSession();
377
- });
378
-
379
- test('keeps the first offer', expectOfferedApplication);
380
-
381
- test('does not initialize with the later offer', () => {
382
- expect(datadogRum.init).not.toHaveBeenCalledWith(
383
- expect.objectContaining({ applicationId: 'later-app-id' })
384
- );
385
- });
386
- });
387
- });
388
- });
389
-
390
- describe('cleanup', () => {
391
- beforeEach(() => {
392
- subject().cleanup();
393
- });
394
-
395
- test('unregisters the service', () => {
396
- expect(capturedState().services.has(options.service.name)).toBe(false);
397
- });
398
- });
399
- });
400
-
401
- describe('setServiceContext', () => {
402
- const name = 'task-hub';
403
- const context = { orgId: '42' };
404
-
405
- beforeEach(() => {
406
- impl.init({ service: { name }, application: minimalApplication });
407
- });
408
-
409
- const subject = () => impl.setServiceContext(name, context);
410
-
411
- test('registers service context', () => {
412
- subject();
413
-
414
- expect(capturedState().services.get(name)?.context).toEqual(context);
415
- });
416
-
417
- describe('when already called', () => {
418
- beforeEach(() => subject());
419
-
420
- test('init() preserves context', () => {
421
- impl.init({ service: { name, beforeSend: jest.fn() } });
422
-
423
- expect(capturedState().services.get(name)?.context).toEqual(context);
424
- });
425
- });
426
- });
427
- });
@@ -1,145 +0,0 @@
1
- import '@testing-library/jest-dom';
2
- import { act, render, screen, waitFor } from '@testing-library/react';
3
- import { FC, Fragment, useState } from 'react';
4
- import { bundleState } from '../bundle-state';
5
- import { DatadogProvider, Options } from '../datadog-provider';
6
- import type { RumUser } from '../set-user';
7
- import { setUser } from '../set-user';
8
-
9
- jest.mock('../set-user');
10
- jest.mock('../hash-routing', () => mockComponent('HashRouting'));
11
-
12
- function mockComponent(name: string) {
13
- return {
14
- [name]: ({ children }: any) => (
15
- <Fragment>
16
- <div>{`<${name} />`}</div>
17
- {children}
18
- </Fragment>
19
- ),
20
- };
21
- }
22
-
23
- describe(`[datadog-rum] ${DatadogProvider.name}`, () => {
24
- const initialUser: RumUser = { id: '1' };
25
-
26
- let isEnabled: boolean;
27
- let options: Options;
28
- let updateUser: (user?: RumUser) => void;
29
-
30
- beforeEach(() => {
31
- jest.clearAllMocks();
32
- isEnabled = true;
33
- options = {};
34
- bundleState.isSdkInitializer = false;
35
- });
36
-
37
- const subject = () => {
38
- const Component: FC = () => {
39
- const [user, setUserInner] = useState<RumUser | undefined>(initialUser);
40
- updateUser = setUserInner;
41
- return <DatadogProvider isEnabled={isEnabled} user={user} options={options} />;
42
- };
43
- return render(<Component />);
44
- };
45
-
46
- function itRendersHashRoutingWhenEnabled() {
47
- test('does not render <HashRouting />', async () => {
48
- subject();
49
-
50
- await waitFor(() =>
51
- expect(screen.queryByText('<HashRouting />')).not.toBeInTheDocument()
52
- );
53
- });
54
-
55
- describe('with options.hashRouting=true', () => {
56
- beforeEach(() => {
57
- options.hashRouting = true;
58
- });
59
-
60
- test('renders <HashRouting />', async () => {
61
- subject();
62
-
63
- await waitFor(() =>
64
- expect(screen.queryByText('<HashRouting />')).toBeInTheDocument()
65
- );
66
- });
67
- });
68
- }
69
-
70
- function itCallsSetUser() {
71
- test('calls setUser', () => {
72
- subject();
73
-
74
- expect(setUser).toHaveBeenCalledWith(initialUser);
75
- });
76
- }
77
-
78
- itCallsSetUser();
79
-
80
- itRendersHashRoutingWhenEnabled();
81
-
82
- describe('when the user is changed', () => {
83
- const nextUser: RumUser = { id: '2' };
84
-
85
- beforeEach(() => {
86
- subject();
87
- act(() => {
88
- updateUser(nextUser);
89
- });
90
- });
91
-
92
- test('calls setUser with the new user', () => {
93
- expect(setUser).toHaveBeenCalledWith(nextUser);
94
- });
95
-
96
- describe('when the user is then unset', () => {
97
- beforeEach(() => {
98
- jest.clearAllMocks();
99
- act(() => {
100
- updateUser(undefined);
101
- });
102
- });
103
-
104
- test('does not call setUser', () => {
105
- expect(setUser).not.toHaveBeenCalled();
106
- });
107
- });
108
- });
109
-
110
- describe('with isEnabled=false', () => {
111
- beforeEach(() => {
112
- isEnabled = false;
113
- });
114
-
115
- test('does not call setUser', () => {
116
- subject();
117
-
118
- expect(setUser).not.toHaveBeenCalled();
119
- });
120
-
121
- describe('with options.hashRouting=true', () => {
122
- beforeEach(() => {
123
- options.hashRouting = true;
124
- });
125
-
126
- test('does not render <HashRouting />', async () => {
127
- subject();
128
-
129
- await waitFor(() =>
130
- expect(screen.queryByText('<HashRouting />')).not.toBeInTheDocument()
131
- );
132
- });
133
- });
134
-
135
- describe('when the bundle is the SDK initializer', () => {
136
- beforeEach(() => {
137
- bundleState.isSdkInitializer = true;
138
- });
139
-
140
- itCallsSetUser();
141
-
142
- itRendersHashRoutingWhenEnabled();
143
- });
144
- });
145
- });
@@ -1,73 +0,0 @@
1
- import type { DatadogRum } from '@datadog/browser-rum';
2
- import { installDdRumGuard } from '../dd-rum-guard';
3
-
4
- describe(`[datadog-rum] ${installDdRumGuard.name}`, () => {
5
- const slot = window as unknown as Record<string, unknown>;
6
-
7
- let instance: DatadogRum;
8
- let errorSpy: jest.SpyInstance;
9
-
10
- beforeEach(() => {
11
- jest.clearAllMocks();
12
- errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
13
- instance = { addAction: jest.fn() } as unknown as DatadogRum;
14
- delete slot.DD_RUM;
15
- });
16
-
17
- afterEach(() => {
18
- delete slot.DD_RUM;
19
- });
20
-
21
- const subject = () => installDdRumGuard(instance);
22
-
23
- test('exposes the initialized instance', () => {
24
- subject();
25
-
26
- expect(slot.DD_RUM).toBe(instance);
27
- });
28
-
29
- describe('when other code reassigns the handle', () => {
30
- beforeEach(() => {
31
- subject();
32
- slot.DD_RUM = { impostor: true };
33
- });
34
-
35
- test('keeps the initialized instance', () => {
36
- expect(slot.DD_RUM).toBe(instance);
37
- });
38
-
39
- test('reports nothing', () => {
40
- expect(instance.addAction).not.toHaveBeenCalled();
41
- });
42
-
43
- describe('when the handle is reassigned multiple times', () => {
44
- beforeEach(() => {
45
- slot.DD_RUM = { impostor: 'again' };
46
- slot.DD_RUM = { impostor: 'and again' };
47
- });
48
-
49
- test('still keeps the initialized instance', () => {
50
- expect(slot.DD_RUM).toBe(instance);
51
- });
52
- });
53
- });
54
-
55
- describe('when the handle cannot be defined', () => {
56
- const error = new Error('Nope!');
57
-
58
- beforeEach(() => {
59
- jest.spyOn(Object, 'defineProperty').mockImplementationOnce(() => {
60
- throw error;
61
- });
62
- });
63
-
64
- test('reports the failure rather than throwing', () => {
65
- subject();
66
-
67
- expect(errorSpy).toHaveBeenCalledWith(
68
- expect.stringContaining('failed to install the window.DD_RUM guard'),
69
- error
70
- );
71
- });
72
- });
73
- });