-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathmicrofrontend.scenario.ts
More file actions
482 lines (389 loc) · 16.3 KB
/
Copy pathmicrofrontend.scenario.ts
File metadata and controls
482 lines (389 loc) · 16.3 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
import type { RumEvent, RumEventDomainContext, RumInitConfiguration } from '@datadog/browser-rum-core'
import type { LogsEvent, LogsInitConfiguration, LogsEventDomainContext } from '@datadog/browser-logs'
import { test, expect } from '@playwright/test'
import { createTest, microfrontendSetup } from '../lib/framework'
import { isLongAnimationFrameSupported } from '../lib/helpers/browser'
const HANDLING_STACK_REGEX = /^HandlingStack: .*\n\s+at testHandlingStack @/
const RUM_CONFIG: Partial<RumInitConfiguration> = {
service: 'main-service',
version: '1.0.0',
beforeSend: (event: RumEvent, domainContext: RumEventDomainContext) => {
if ('handlingStack' in domainContext) {
event.context!.handlingStack = domainContext.handlingStack
}
return true
},
}
const LOGS_CONFIG: Partial<LogsInitConfiguration> = {
forwardConsoleLogs: 'all',
beforeSend: (event: LogsEvent, domainContext: LogsEventDomainContext) => {
if (domainContext && 'handlingStack' in domainContext) {
event.context = { handlingStack: domainContext.handlingStack }
}
return true
},
}
test.describe('microfrontend', () => {
test.describe('RUM', () => {
test.describe('with beforeSend', () => {
createTest('expose handling stack for fetch requests')
.withRum(RUM_CONFIG)
.withRumInit((configuration) => {
window.DD_RUM!.init(configuration)
// eslint-disable-next-line @typescript-eslint/no-empty-function
const noop = () => {}
function testHandlingStack() {
fetch('/ok').then(noop, noop)
}
testHandlingStack()
})
.run(async ({ intakeRegistry, flushEvents }) => {
await flushEvents()
const event = intakeRegistry.rumResourceEvents.find((event) => event.resource.type === 'fetch')
expect(event).toBeTruthy()
expect(event?.context?.handlingStack).toMatch(HANDLING_STACK_REGEX)
})
createTest('expose handling stack for xhr requests')
.withRum(RUM_CONFIG)
.withRumInit((configuration) => {
window.DD_RUM!.init(configuration)
function testHandlingStack() {
const xhr = new XMLHttpRequest()
xhr.open('GET', '/ok')
xhr.send()
}
testHandlingStack()
})
.run(async ({ intakeRegistry, flushEvents }) => {
await flushEvents()
const event = intakeRegistry.rumResourceEvents.find((event) => event.resource.type === 'xhr')
expect(event).toBeTruthy()
expect(event?.context?.handlingStack).toMatch(HANDLING_STACK_REGEX)
})
createTest('expose handling stack for DD_RUM.addAction')
.withRum(RUM_CONFIG)
.withRumInit((configuration) => {
window.DD_RUM!.init(configuration)
function testHandlingStack() {
window.DD_RUM!.addAction('foo')
}
testHandlingStack()
})
.run(async ({ intakeRegistry, flushEvents }) => {
await flushEvents()
const event = intakeRegistry.rumActionEvents[0]
expect(event).toBeTruthy()
expect(event?.context?.handlingStack).toMatch(HANDLING_STACK_REGEX)
})
createTest('expose handling stack for DD_RUM.addError')
.withRum(RUM_CONFIG)
.withRumInit((configuration) => {
window.DD_RUM!.init(configuration)
function testHandlingStack() {
window.DD_RUM!.addError(new Error('foo'))
}
testHandlingStack()
})
.run(async ({ intakeRegistry, flushEvents }) => {
await flushEvents()
const event = intakeRegistry.rumErrorEvents[0]
expect(event).toBeTruthy()
expect(event?.context?.handlingStack).toMatch(HANDLING_STACK_REGEX)
})
createTest('expose handling stack for console errors')
.withRum(RUM_CONFIG)
.withRumInit((configuration) => {
window.DD_RUM!.init(configuration)
function testHandlingStack() {
console.error('foo')
}
testHandlingStack()
})
.run(async ({ intakeRegistry, flushEvents, withBrowserLogs }) => {
await flushEvents()
const event = intakeRegistry.rumErrorEvents[0]
withBrowserLogs((logs) => {
expect(logs).toHaveLength(1)
expect(logs[0].message).toMatch(/foo$/)
})
expect(event).toBeTruthy()
expect(event?.context?.handlingStack).toMatch(HANDLING_STACK_REGEX)
})
createTest('expose handling stack for DD_RUM.startView')
.withRum(RUM_CONFIG)
.withRumInit((configuration) => {
window.DD_RUM!.init(configuration)
function testHandlingStack() {
window.DD_RUM!.startView({ name: 'test-view' })
}
testHandlingStack()
})
.run(async ({ intakeRegistry, flushEvents }) => {
await flushEvents()
const event = intakeRegistry.rumViewEvents.find((event) => event.view.name === 'test-view')
expect(event).toBeTruthy()
expect(event?.context?.handlingStack).toMatch(HANDLING_STACK_REGEX)
})
createTest('expose handling stack for DD_RUM.startDurationVital')
.withRum(RUM_CONFIG)
.withRumInit((configuration) => {
window.DD_RUM!.init(configuration)
function testHandlingStack() {
window.DD_RUM!.startDurationVital('test-vital')
window.DD_RUM!.stopDurationVital('test-vital')
}
testHandlingStack()
})
.run(async ({ intakeRegistry, flushEvents }) => {
await flushEvents()
const event = intakeRegistry.rumVitalEvents.find((event) => event.vital.name === 'test-vital')
expect(event).toBeTruthy()
expect(event?.context?.handlingStack).toMatch(HANDLING_STACK_REGEX)
})
createTest('expose handling stack for DD_RUM.startFeatureOperation')
.withRum({ ...RUM_CONFIG })
.withRumInit((configuration) => {
window.DD_RUM!.init(configuration)
function testHandlingStack() {
window.DD_RUM!.startFeatureOperation('test-operation')
}
testHandlingStack()
})
.run(async ({ intakeRegistry, flushEvents }) => {
await flushEvents()
const event = intakeRegistry.rumVitalEvents.find((event) => event.vital.name === 'test-operation')
expect(event).toBeTruthy()
expect(event?.context?.handlingStack).toMatch(HANDLING_STACK_REGEX)
})
createTest('resource: allow to modify service and version')
.withRum(RUM_CONFIG)
.withRumInit((configuration) => {
window.DD_RUM!.init({
...configuration,
beforeSend: (event: RumEvent) => {
if (event.type === 'resource') {
event.service = 'mfe-service'
event.version = '0.1.0'
}
return true
},
})
})
.run(async ({ intakeRegistry, flushEvents }) => {
await flushEvents()
const viewEvent = intakeRegistry.rumViewEvents[0]
const resourceEvent = intakeRegistry.rumResourceEvents[0]
expect(viewEvent).toBeTruthy()
expect(viewEvent.service).toBe('main-service')
expect(viewEvent.version).toBe('1.0.0')
expect(resourceEvent).toBeTruthy()
expect(resourceEvent.service).toBe('mfe-service')
expect(resourceEvent.version).toBe('0.1.0')
})
createTest('view: allowed to modify service and version')
.withRum(RUM_CONFIG)
.withRumInit((configuration) => {
window.DD_RUM!.init({
...configuration,
beforeSend: (event: RumEvent) => {
if (event.type === 'view') {
event.service = 'mfe-service'
event.version = '0.1.0'
}
return true
},
})
})
.run(async ({ intakeRegistry, flushEvents }) => {
await flushEvents()
const viewEvent = intakeRegistry.rumViewEvents[0]
expect(viewEvent).toBeTruthy()
expect(viewEvent.service).toBe('mfe-service')
expect(viewEvent.version).toBe('0.1.0')
})
})
test.describe('with source code bundler plugin', () => {
createTest('errors from console.error should have service and version from source code context')
.withRum(RUM_CONFIG)
.withSetup(microfrontendSetup)
.run(async ({ intakeRegistry, flushEvents, page, withBrowserLogs }) => {
await page.click('#app1-console-error')
await page.click('#app2-console-error')
await flushEvents()
expect(intakeRegistry.rumErrorEvents).toMatchObject([
expect.objectContaining({ service: 'mfe-app1-service', version: '1.0.0' }),
expect.objectContaining({ service: 'mfe-app2-service', version: '0.2.0' }),
])
withBrowserLogs((browserLogs) => {
expect(browserLogs).toHaveLength(2)
})
})
createTest('runtime errors should have service and version from source code context')
.withRum(RUM_CONFIG)
.withSetup(microfrontendSetup)
.run(async ({ intakeRegistry, flushEvents, page, withBrowserLogs }) => {
await page.click('#app1-runtime-error')
await page.click('#app2-runtime-error')
await flushEvents()
expect(intakeRegistry.rumErrorEvents).toMatchObject([
expect.objectContaining({ service: 'mfe-app1-service', version: '1.0.0' }),
expect.objectContaining({ service: 'mfe-app2-service', version: '0.2.0' }),
])
withBrowserLogs((browserLogs) => {
expect(browserLogs).toHaveLength(2)
})
})
createTest('fetch requests should have service and version from source code context')
.withRum(RUM_CONFIG)
.withSetup(microfrontendSetup)
.run(async ({ intakeRegistry, flushEvents, page }) => {
await page.click('#app1-fetch')
await page.click('#app2-fetch')
await flushEvents()
const resourceEvents = intakeRegistry.rumResourceEvents.filter((event) => event.resource.type === 'fetch')
expect(resourceEvents).toMatchObject([
expect.objectContaining({ service: 'mfe-app1-service', version: '1.0.0' }),
expect.objectContaining({ service: 'mfe-app2-service', version: '0.2.0' }),
])
})
createTest('xhr requests should have service and version from source code context')
.withRum(RUM_CONFIG)
.withSetup(microfrontendSetup)
.run(async ({ intakeRegistry, flushEvents, page }) => {
await page.click('#app1-xhr')
await page.click('#app2-xhr')
await flushEvents()
const resourceEvents = intakeRegistry.rumResourceEvents.filter((event) => event.resource.type === 'xhr')
expect(resourceEvents).toMatchObject([
expect.objectContaining({ service: 'mfe-app1-service', version: '1.0.0' }),
expect.objectContaining({ service: 'mfe-app2-service', version: '0.2.0' }),
])
})
createTest('custom actions should have service and version from source code context')
.withRum(RUM_CONFIG)
.withSetup(microfrontendSetup)
.run(async ({ intakeRegistry, flushEvents, page }) => {
await page.click('#app1-custom-action')
await page.click('#app2-custom-action')
await flushEvents()
const rumActionEvents = intakeRegistry.rumActionEvents.filter((event) => event.action.type === 'custom')
expect(rumActionEvents).toMatchObject([
expect.objectContaining({
service: 'mfe-app1-service',
version: '1.0.0',
}),
expect.objectContaining({
service: 'mfe-app2-service',
version: '0.2.0',
}),
])
})
createTest('LOAf should have service and version from source code context')
.withRum(RUM_CONFIG)
.withSetup(microfrontendSetup)
.run(async ({ intakeRegistry, flushEvents, page }) => {
test.skip(
!(await isLongAnimationFrameSupported(page)),
'Browser does not support PerformanceLongAnimationFrameTiming'
)
await page.click('#app1-loaf')
await page.click('#app2-loaf')
await flushEvents()
const longTaskEvents = intakeRegistry.rumLongTaskEvents.filter((event) =>
event.long_task.scripts?.[0]?.invoker?.includes('onclick')
)
expect(longTaskEvents).toMatchObject([
expect.objectContaining({ service: 'mfe-app1-service', version: '1.0.0' }),
expect.objectContaining({ service: 'mfe-app2-service', version: '0.2.0' }),
])
})
createTest('manual views should have service and version from source code context')
.withRum(RUM_CONFIG)
.withSetup(microfrontendSetup)
.run(async ({ intakeRegistry, flushEvents, page }) => {
await page.click('#app1-view')
await page.click('#app2-view')
await flushEvents()
expect(intakeRegistry.rumViewEvents).toMatchObject(
expect.arrayContaining([
expect.objectContaining({
view: expect.objectContaining({ name: 'app1-view' }),
service: 'mfe-app1-service',
version: '1.0.0',
}),
expect.objectContaining({
view: expect.objectContaining({ name: 'app2-view' }),
service: 'mfe-app2-service',
version: '0.2.0',
}),
])
)
})
createTest('duration vitals should have service and version from source code context')
.withRum(RUM_CONFIG)
.withSetup(microfrontendSetup)
.run(async ({ intakeRegistry, flushEvents, page }) => {
await page.click('#app1-vital')
await page.click('#app2-vital')
await flushEvents()
expect(intakeRegistry.rumVitalEvents).toMatchObject([
expect.objectContaining({ service: 'mfe-app1-service', version: '1.0.0' }),
expect.objectContaining({ service: 'mfe-app2-service', version: '0.2.0' }),
])
})
createTest('feature operations should have service and version from source code context')
.withRum({ ...RUM_CONFIG })
.withSetup(microfrontendSetup)
.run(async ({ intakeRegistry, flushEvents, page }) => {
await page.click('#app1-feature-operation')
await page.click('#app2-feature-operation')
await flushEvents()
const featureOperationEvents = intakeRegistry.rumVitalEvents.filter(
(event) => event.vital.step_type === 'start'
)
expect(featureOperationEvents).toMatchObject([
expect.objectContaining({ service: 'mfe-app1-service', version: '1.0.0' }),
expect.objectContaining({ service: 'mfe-app2-service', version: '0.2.0' }),
])
})
})
})
test.describe('Logs', () => {
createTest('expose handling stack for console.log')
.withLogs(LOGS_CONFIG)
.withLogsInit((configuration) => {
window.DD_LOGS!.init(configuration)
function testHandlingStack() {
console.log('foo')
}
testHandlingStack()
})
.run(async ({ intakeRegistry, flushEvents, flushBrowserLogs }) => {
await flushEvents()
const event = intakeRegistry.logsEvents[0]
flushBrowserLogs()
expect(event).toBeTruthy()
expect(event?.context).toEqual({
handlingStack: expect.stringMatching(HANDLING_STACK_REGEX),
})
})
createTest('expose handling stack for DD_LOGS.logger.log')
.withLogs(LOGS_CONFIG)
.withLogsInit((configuration) => {
window.DD_LOGS!.init(configuration)
function testHandlingStack() {
window.DD_LOGS!.logger.log('foo')
}
testHandlingStack()
})
.run(async ({ intakeRegistry, flushEvents, flushBrowserLogs }) => {
await flushEvents()
const event = intakeRegistry.logsEvents[0]
flushBrowserLogs()
expect(event).toBeTruthy()
expect(event?.context).toEqual({
handlingStack: expect.stringMatching(HANDLING_STACK_REGEX),
})
})
})
})