-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathexpect.ts
More file actions
82 lines (76 loc) · 2.47 KB
/
expect.ts
File metadata and controls
82 lines (76 loc) · 2.47 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
import type { Contexts, Envelope, Event, SdkInfo } from '@sentry/core';
import { SDK_VERSION } from '@sentry/core';
import { expect } from 'vitest';
export const UUID_MATCHER = expect.stringMatching(/^[\da-f]{32}$/);
export const UUID_V4_MATCHER = expect.stringMatching(
/^[\da-f]{8}-[\da-f]{4}-4[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$/,
);
export const SHORT_UUID_MATCHER = expect.stringMatching(/^[\da-f]{16}$/);
export const ISO_DATE_MATCHER = expect.stringMatching(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/);
function dropUndefinedKeys<T extends Record<string, unknown>>(obj: T): T {
for (const [key, value] of Object.entries(obj)) {
if (value === undefined) {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete obj[key];
}
}
return obj;
}
function getSdk(sdk: 'cloudflare' | 'hono'): SdkInfo {
return {
integrations: expect.any(Array),
name: `sentry.javascript.${sdk}`,
packages: [
{
name: `npm:@sentry/${sdk}`,
version: SDK_VERSION,
},
],
version: SDK_VERSION,
};
}
function defaultContexts(eventContexts: Contexts = {}): Contexts {
return dropUndefinedKeys({
trace: {
trace_id: UUID_MATCHER,
span_id: SHORT_UUID_MATCHER,
},
cloud_resource: { 'cloud.provider': 'cloudflare' },
culture: { timezone: expect.any(String) },
runtime: { name: 'cloudflare' },
...eventContexts,
});
}
export function expectedEvent(event: Event, { sdk }: { sdk: 'cloudflare' | 'hono' }): Event {
return dropUndefinedKeys({
event_id: UUID_MATCHER,
timestamp: expect.any(Number),
environment: 'production',
platform: 'javascript',
sdk: getSdk(sdk),
...event,
contexts: defaultContexts(event.contexts),
});
}
export function eventEnvelope(
event: Event,
{ includeSampleRand = false, sdk = 'cloudflare' }: { includeSampleRand?: boolean; sdk?: 'cloudflare' | 'hono' } = {},
): Envelope {
return [
{
event_id: UUID_MATCHER,
sent_at: ISO_DATE_MATCHER,
sdk: { name: `sentry.javascript.${sdk}`, version: SDK_VERSION },
trace: {
environment: event.environment || 'production',
public_key: 'public',
trace_id: UUID_MATCHER,
sample_rate: expect.any(String),
...(includeSampleRand && { sample_rand: expect.stringMatching(/^[01](\.\d+)?$/) }),
sampled: expect.any(String),
transaction: expect.any(String),
},
},
[[{ type: 'event' }, expectedEvent(event, { sdk })]],
];
}