forked from nodejs/node
/
test-cli-print-promise.mjs
152 lines (131 loc) · 3.72 KB
/
test-cli-print-promise.mjs
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
import { spawnPromisified } from '../common/index.mjs';
import assert from 'node:assert';
import { execPath } from 'node:process';
import { describe, it } from 'node:test';
describe('--print with a promise', { concurrency: !process.env.TEST_PARALLEL }, () => {
it('should handle directly-fulfilled promises', async () => {
const result = await spawnPromisified(execPath, [
'--print',
'Promise.resolve(42)',
]);
assert.deepStrictEqual(result, {
code: 0,
signal: null,
stderr: '',
stdout: 'Promise { 42 }\n',
});
});
it('should handle promises fulfilled after one tick', async () => {
const result = await spawnPromisified(execPath, [
'--print',
'Promise.resolve().then(()=>42)',
]);
assert.deepStrictEqual(result, {
code: 0,
signal: null,
stderr: '',
stdout: 'Promise { 42 }\n',
});
});
it('should handle promise that never settles', async () => {
const result = await spawnPromisified(execPath, [
'--print',
'new Promise(()=>{})',
]);
assert.deepStrictEqual(result, {
code: 0,
signal: null,
stderr: '',
stdout: 'Promise { <pending> }\n',
});
});
it('should output something if process exits before promise settles', async () => {
const result = await spawnPromisified(execPath, [
'--print',
'setTimeout(process.exit, 100, 0);timers.promises.setTimeout(200)',
]);
assert.deepStrictEqual(result, {
code: 0,
signal: null,
stderr: '',
stdout: 'Promise { <pending> }\n',
});
});
it('should respect exit code when process exits before promise settles', async () => {
const result = await spawnPromisified(execPath, [
'--print',
'setTimeout(process.exit, 100, 42);timers.promises.setTimeout(200)',
]);
assert.deepStrictEqual(result, {
code: 42,
signal: null,
stderr: '',
stdout: 'Promise { <pending> }\n',
});
});
it('should handle rejected promises', async () => {
const result = await spawnPromisified(execPath, [
'--unhandled-rejections=none',
'--print',
'Promise.reject(1)',
]);
assert.deepStrictEqual(result, {
code: 0,
signal: null,
stderr: '',
stdout: 'Promise { <rejected> 1 }\n',
});
});
it('should handle promises that reject after one tick', async () => {
const result = await spawnPromisified(execPath, [
'--unhandled-rejections=none',
'--print',
'Promise.resolve().then(()=>Promise.reject(1))',
]);
assert.deepStrictEqual(result, {
code: 0,
signal: null,
stderr: '',
stdout: 'Promise { <rejected> 1 }\n',
});
});
it('should handle thenable that resolves', async () => {
const result = await spawnPromisified(execPath, [
'--unhandled-rejections=none',
'--print',
'({ then(r) { r(42) } })',
]);
assert.deepStrictEqual(result, {
code: 0,
signal: null,
stderr: '',
stdout: '{ then: [Function: then] }\n',
});
});
it('should handle thenable that rejects', async () => {
const result = await spawnPromisified(execPath, [
'--unhandled-rejections=none',
'--print',
'({ then(_, r) { r(42) } })',
]);
assert.deepStrictEqual(result, {
code: 0,
signal: null,
stderr: '',
stdout: '{ then: [Function: then] }\n',
});
});
it('should handle Promise.prototype', async () => {
const result = await spawnPromisified(execPath, [
'--unhandled-rejections=none',
'--print',
'Promise.prototype',
]);
assert.deepStrictEqual(result, {
code: 0,
signal: null,
stderr: '',
stdout: 'Object [Promise] {}\n',
});
});
});