-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathtest.ts
More file actions
209 lines (178 loc) · 6.78 KB
/
test.ts
File metadata and controls
209 lines (178 loc) · 6.78 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
import type { Event } from '@sentry/node';
import * as childProcess from 'child_process';
import * as path from 'path';
import { afterAll, describe, expect, test } from 'vitest';
import { cleanupChildProcesses, createRunner } from '../../../utils/runner';
describe('onUnhandledRejectionIntegration', () => {
afterAll(() => {
cleanupChildProcesses();
});
test('should show string-type promise rejection warnings by default', () =>
new Promise<void>(done => {
expect.assertions(3);
const testScriptPath = path.resolve(__dirname, 'mode-warn-string.js');
childProcess.execFile('node', [testScriptPath], { encoding: 'utf8' }, (err, stdout, stderr) => {
expect(err).toBeNull();
expect(stdout).toBe("I'm alive!");
expect(stderr.trim())
.toBe(`This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason:
test rejection`);
done();
});
}));
test('should show error-type promise rejection warnings by default', () =>
new Promise<void>(done => {
expect.assertions(3);
const testScriptPath = path.resolve(__dirname, 'mode-warn-error.js');
childProcess.execFile('node', [testScriptPath], { encoding: 'utf8' }, (err, stdout, stderr) => {
expect(err).toBeNull();
expect(stdout).toBe("I'm alive!");
expect(stderr)
.toContain(`This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason:
Error: test rejection
at Object.<anonymous>`);
done();
});
}));
test('should not close process on unhandled rejection in strict mode', () =>
new Promise<void>(done => {
expect.assertions(4);
const testScriptPath = path.resolve(__dirname, 'mode-strict.js');
childProcess.execFile('node', [testScriptPath], { encoding: 'utf8' }, (err, stdout, stderr) => {
expect(err).not.toBeNull();
expect(err?.code).toBe(1);
expect(stdout).not.toBe("I'm alive!");
expect(stderr)
.toContain(`This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason:
test rejection`);
done();
});
}));
test('should not close process or warn on unhandled rejection in none mode', () =>
new Promise<void>(done => {
expect.assertions(3);
const testScriptPath = path.resolve(__dirname, 'mode-none.js');
childProcess.execFile('node', [testScriptPath], { encoding: 'utf8' }, (err, stdout, stderr) => {
expect(err).toBeNull();
expect(stdout).toBe("I'm alive!");
expect(stderr).toBe('');
done();
});
}));
test('captures exceptions for unhandled rejections', async () => {
await createRunner(__dirname, 'scenario-warn.ts')
.expect({
event: {
level: 'error',
exception: {
values: [
{
type: 'Error',
value: 'test rejection',
mechanism: {
type: 'auto.node.onunhandledrejection',
handled: false,
},
stacktrace: {
frames: expect.any(Array),
},
},
],
},
},
})
.start()
.completed();
});
test('captures exceptions for unhandled rejections in strict mode', async () => {
await createRunner(__dirname, 'scenario-strict.ts')
.expect({
event: {
level: 'fatal',
exception: {
values: [
{
type: 'Error',
value: 'test rejection',
mechanism: {
type: 'auto.node.onunhandledrejection',
handled: false,
},
stacktrace: {
frames: expect.any(Array),
},
},
],
},
},
})
.start()
.completed();
});
test('handles unhandled rejection in spans', async () => {
let transactionEvent: Event | undefined;
let errorEvent: Event | undefined;
await createRunner(__dirname, 'scenario-with-span.ts')
.expect({
transaction: transaction => {
transactionEvent = transaction;
},
})
.expect({
event: event => {
errorEvent = event;
},
})
.start()
.completed();
expect(transactionEvent).toBeDefined();
expect(errorEvent).toBeDefined();
expect(transactionEvent!.transaction).toBe('test-span');
expect(transactionEvent!.contexts!.trace!.trace_id).toBe(errorEvent!.contexts!.trace!.trace_id);
expect(transactionEvent!.contexts!.trace!.span_id).toBe(errorEvent!.contexts!.trace!.span_id);
});
test('handles unhandled rejection in spans that are ended early', async () => {
let transactionEvent: Event | undefined;
let errorEvent: Event | undefined;
await createRunner(__dirname, 'scenario-with-span-ended.ts')
.expect({
transaction: transaction => {
transactionEvent = transaction;
},
})
.expect({
event: event => {
errorEvent = event;
},
})
.start()
.completed();
expect(transactionEvent).toBeDefined();
expect(errorEvent).toBeDefined();
expect(transactionEvent!.transaction).toBe('test-span');
expect(transactionEvent!.contexts!.trace!.trace_id).toBe(errorEvent!.contexts!.trace!.trace_id);
expect(transactionEvent!.contexts!.trace!.span_id).toBe(errorEvent!.contexts!.trace!.span_id);
});
test('should not warn when AI_NoOutputGeneratedError or AbortError is rejected (default ignore)', () =>
new Promise<void>(done => {
expect.assertions(3);
const testScriptPath = path.resolve(__dirname, 'ignore-default.js');
childProcess.execFile('node', [testScriptPath], { encoding: 'utf8' }, (err, stdout, stderr) => {
expect(err).toBeNull();
expect(stdout).toBe("I'm alive!");
expect(stderr).toBe(''); // No warning should be shown
done();
});
}));
test('should not warn when custom ignored error by name is rejected', () =>
new Promise<void>(done => {
expect.assertions(3);
const testScriptPath = path.resolve(__dirname, 'ignore-custom-name.js');
childProcess.execFile('node', [testScriptPath], { encoding: 'utf8' }, (err, stdout, stderr) => {
expect(err).toBeNull();
expect(stdout).toBe("I'm alive!");
expect(stderr).toBe(''); // No warning should be shown
done();
});
}));
});