-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathtest.ts
127 lines (112 loc) · 4.17 KB
/
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
127
import * as childProcess from 'child_process';
import * as path from 'path';
import { afterAll, describe, expect, test } from 'vitest';
import { cleanupChildProcesses } from '../../../utils/runner';
import { 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: '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: 'onunhandledrejection',
handled: false,
},
stacktrace: {
frames: expect.any(Array),
},
},
],
},
},
})
.start()
.completed();
});
});