-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathbunserver.test.ts
126 lines (101 loc) · 3.37 KB
/
bunserver.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
import { Hub, makeMain } from '@sentry/core';
// eslint-disable-next-line import/no-unresolved
import { beforeAll, beforeEach, describe, expect, test } from 'bun:test';
import { BunClient } from '../../src/client';
import { instrumentBunServe } from '../../src/integrations/bunserver';
import { getDefaultBunClientOptions } from '../helpers';
// Fun fact: Bun = 2 21 14 :)
const DEFAULT_PORT = 22114;
describe('Bun Serve Integration', () => {
let hub: Hub;
let client: BunClient;
beforeAll(() => {
instrumentBunServe();
});
beforeEach(() => {
const options = getDefaultBunClientOptions({ tracesSampleRate: 1, debug: true });
client = new BunClient(options);
hub = new Hub(client);
makeMain(hub);
});
test('generates a transaction around a request', async () => {
client.on('finishTransaction', transaction => {
expect(transaction.status).toBe('ok');
expect(transaction.tags).toEqual({
'http.status_code': '200',
});
expect(transaction.op).toEqual('http.server');
expect(transaction.name).toEqual('GET /');
});
const server = Bun.serve({
async fetch(_req) {
return new Response('Bun!');
},
port: DEFAULT_PORT,
});
await fetch('http://localhost:22114/');
server.stop();
});
test('generates a post transaction', async () => {
client.on('finishTransaction', transaction => {
expect(transaction.status).toBe('ok');
expect(transaction.tags).toEqual({
'http.status_code': '200',
});
expect(transaction.op).toEqual('http.server');
expect(transaction.name).toEqual('POST /');
});
const server = Bun.serve({
async fetch(_req) {
return new Response('Bun!');
},
port: DEFAULT_PORT,
});
await fetch('http://localhost:22114/', {
method: 'POST',
});
server.stop();
});
test('continues a trace', async () => {
const TRACE_ID = '12312012123120121231201212312012';
const PARENT_SPAN_ID = '1121201211212012';
const PARENT_SAMPLED = '1';
const SENTRY_TRACE_HEADER = `${TRACE_ID}-${PARENT_SPAN_ID}-${PARENT_SAMPLED}`;
const SENTRY_BAGGAGE_HEADER = 'sentry-version=1.0,sentry-environment=production';
client.on('finishTransaction', transaction => {
expect(transaction.traceId).toBe(TRACE_ID);
expect(transaction.parentSpanId).toBe(PARENT_SPAN_ID);
expect(transaction.sampled).toBe(true);
expect(transaction.metadata?.dynamicSamplingContext).toStrictEqual({ version: '1.0', environment: 'production' });
});
const server = Bun.serve({
async fetch(_req) {
return new Response('Bun!');
},
port: DEFAULT_PORT,
});
await fetch('http://localhost:22114/', {
headers: { 'sentry-trace': SENTRY_TRACE_HEADER, baggage: SENTRY_BAGGAGE_HEADER },
});
server.stop();
});
test('does not create transactions for OPTIONS or HEAD requests', async () => {
client.on('finishTransaction', () => {
// This will never run, but we want to make sure it doesn't run.
expect(false).toEqual(true);
});
const server = Bun.serve({
async fetch(_req) {
return new Response('Bun!');
},
port: DEFAULT_PORT,
});
await fetch('http://localhost:22114/', {
method: 'OPTIONS',
});
await fetch('http://localhost:22114/', {
method: 'HEAD',
});
server.stop();
});
});