-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathtest.ts
More file actions
180 lines (161 loc) · 6.21 KB
/
test.ts
File metadata and controls
180 lines (161 loc) · 6.21 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
import { afterAll, describe, expect } from 'vitest';
import { cleanupChildProcesses, createEsmAndCjsTests } from '../../utils/runner';
describe('vercel', () => {
afterAll(() => {
cleanupChildProcesses();
});
createEsmAndCjsTests(__dirname, 'scenario.mjs', 'instrument.mjs', (createRunner, test) => {
test('should flush spans correctly on Vercel', async () => {
const runner = createRunner()
.expect({
transaction: {
transaction: 'GET /test/express',
},
})
.start();
runner.makeRequest('get', '/test/express');
await runner.completed();
const actualLogs = runner.getLogs();
// We want to test that the following logs are present in this order
// other logs may be in between
const expectedLogs = [
'Sentry Logger [log]: @sentry/instrumentation-http Patching server.emit',
'Sentry Logger [log]: @sentry/instrumentation-http Handling incoming request',
'Sentry Logger [log]: @sentry/instrumentation-http Patching request.on',
'Sentry Logger [debug]: @opentelemetry_sentry-patched/instrumentation-http http instrumentation incomingRequest',
'Sentry Logger [log]: [Tracing] Starting sampled root span',
// later...
'Sentry Logger [log]: Patching response to flush on Vercel',
'Sentry Logger [log]: Patching res.end()',
// later...
'Sentry Logger [log]: Flushing events before Vercel Lambda freeze',
'Sentry Logger [log]: SpanExporter exported 4 spans, 0 spans are waiting for their parent spans to finish',
];
// Test that the order of logs is correct
for (const log of actualLogs) {
if (expectedLogs.length === 0) {
break;
}
if (log === expectedLogs[0]) {
expectedLogs.shift();
}
}
if (expectedLogs.length > 0) {
// eslint-disable-next-line no-console
console.log(actualLogs);
expect(expectedLogs).toEqual([]);
}
});
test('should flush errors correctly on Vercel', async () => {
const runner = createRunner()
.expect({
transaction: {
transaction: 'GET /test/error',
},
})
.expect({
event: {
transaction: 'GET /test/error',
exception: {
values: [
{
value: 'test error',
},
],
},
},
})
.start();
runner.makeRequest('get', '/test/error', { expectError: true });
await runner.completed();
const actualLogs = runner.getLogs();
// We want to test that the following logs are present in this order
// other logs may be in between
const expectedLogs = [
'Sentry Logger [log]: @sentry/instrumentation-http Patching server.emit',
'Sentry Logger [log]: @sentry/instrumentation-http Handling incoming request',
'Sentry Logger [log]: @sentry/instrumentation-http Patching request.on',
'Sentry Logger [debug]: @opentelemetry_sentry-patched/instrumentation-http http instrumentation incomingRequest',
'Sentry Logger [log]: [Tracing] Starting sampled root span',
// later...
'Sentry Logger [log]: Patching response to flush on Vercel',
'Sentry Logger [log]: Patching res.end()',
// later...
'Sentry Logger [log]: Captured error event `test error`',
// later...
'Sentry Logger [log]: Flushing events before Vercel Lambda freeze',
'Sentry Logger [log]: SpanExporter exported 4 spans, 0 spans are waiting for their parent spans to finish',
];
// Test that the order of logs is correct
for (const log of actualLogs) {
if (expectedLogs.length === 0) {
break;
}
if (log === expectedLogs[0]) {
expectedLogs.shift();
}
}
if (expectedLogs.length > 0) {
// eslint-disable-next-line no-console
console.log(actualLogs);
expect(expectedLogs).toEqual([]);
}
});
});
describe('without http.server spans', () => {
createEsmAndCjsTests(
__dirname,
'scenario-withoutSpans.mjs',
'instrument-withoutSpans.mjs',
(createRunner, test) => {
test('should flush errors correctly on Vercel even without HTTP span instrumentation', async () => {
const runner = createRunner()
.expect({
event: {
transaction: 'GET /test/error',
exception: {
values: [
{
value: 'test error',
},
],
},
},
})
.start();
runner.makeRequest('get', '/test/error', { expectError: true });
await runner.completed();
const actualLogs = runner.getLogs();
// We want to test that the following logs are present in this order
// other logs may be in between
const expectedLogs = [
'Sentry Logger [log]: @sentry/instrumentation-http Patching server.emit',
'Sentry Logger [log]: Patching response to flush on Vercel',
'Sentry Logger [log]: Patching res.end()',
'Sentry Logger [log]: @sentry/instrumentation-http Handling incoming request',
'Sentry Logger [log]: @sentry/instrumentation-http Patching request.on',
// later...
'Sentry Logger [log]: Captured error event `test error`',
// later...
'Sentry Logger [log]: Flushing events before Vercel Lambda freeze',
'Sentry Logger [log]: SpanExporter exported 0 spans, 0 spans are waiting for their parent spans to finish',
];
// Test that the order of logs is correct
for (const log of actualLogs) {
if (expectedLogs.length === 0) {
break;
}
if (log === expectedLogs[0]) {
expectedLogs.shift();
}
}
if (expectedLogs.length > 0) {
// eslint-disable-next-line no-console
console.log(actualLogs);
expect(expectedLogs).toEqual([]);
}
});
},
);
});
});