forked from nodejs/node
/
test-esm-tla-unfinished.mjs
161 lines (133 loc) · 5.41 KB
/
test-esm-tla-unfinished.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
153
154
155
156
157
158
159
160
161
import { spawnPromisified } from '../common/index.mjs';
import fixtures from '../common/fixtures.js';
import assert from 'node:assert';
import { execPath } from 'node:process';
import { describe, it } from 'node:test';
const commonArgs = [
'--input-type=module',
'--eval',
];
describe('ESM: unsettled and rejected promises', { concurrency: !process.env.TEST_PARALLEL }, () => {
it('should exit for an unsettled TLA promise via --eval with a warning', async () => {
const { code, stderr, stdout } = await spawnPromisified(execPath, [
...commonArgs,
'await new Promise(() => {})',
]);
assert.match(stderr, /Warning: Detected unsettled top-level await at.+\[eval1\]:1/);
assert.match(stderr, /await new Promise/);
assert.strictEqual(stdout, '');
assert.strictEqual(code, 13);
});
it('should exit for an unsettled TLA promise via --eval without warning', async () => {
const { code, stderr, stdout } = await spawnPromisified(execPath, [
'--no-warnings',
...commonArgs,
'await new Promise(() => {})',
]);
assert.strictEqual(stderr, '');
assert.strictEqual(stdout, '');
assert.strictEqual(code, 13);
});
it('should throw for a rejected TLA promise via --eval', async () => {
// Rejected TLA promise, --eval
const { code, stderr, stdout } = await spawnPromisified(execPath, [
...commonArgs,
'await Promise.reject(new Error("Xyz"))',
]);
assert.match(stderr, /Error: Xyz/);
assert.strictEqual(stdout, '');
assert.strictEqual(code, 1);
});
it('should exit for an unsettled TLA promise and respect explicit exit code via --eval', async () => {
// Rejected TLA promise, --eval
const { code, stderr, stdout } = await spawnPromisified(execPath, [
...commonArgs,
'process.exitCode = 42;await new Promise(() => {})',
]);
assert.strictEqual(stderr, '');
assert.strictEqual(stdout, '');
assert.strictEqual(code, 42);
});
it('should throw for a rejected TLA promise and ignore explicit exit code via --eval', async () => {
// Rejected TLA promise, --eval
const { code, stderr, stdout } = await spawnPromisified(execPath, [
...commonArgs,
'process.exitCode = 42;await Promise.reject(new Error("Xyz"))',
]);
assert.match(stderr, /Error: Xyz/);
assert.strictEqual(stdout, '');
assert.strictEqual(code, 1);
});
it('should exit for an unsettled TLA promise with warning', async () => {
const { code, stderr, stdout } = await spawnPromisified(execPath, [
fixtures.path('es-modules/tla/unresolved.mjs'),
]);
assert.match(stderr, /Warning: Detected unsettled top-level await at.+unresolved\.mjs:1/);
assert.match(stderr, /await new Promise/);
assert.strictEqual(stdout, '');
assert.strictEqual(code, 13);
});
it('should exit for an unsettled TLA promise without warning', async () => {
const { code, stderr, stdout } = await spawnPromisified(execPath, [
'--no-warnings',
fixtures.path('es-modules/tla/unresolved.mjs'),
]);
assert.strictEqual(stderr, '');
assert.strictEqual(stdout, '');
assert.strictEqual(code, 13);
});
it('should throw for a rejected TLA promise via stdin', async () => {
const { code, stderr, stdout } = await spawnPromisified(execPath, [
'--no-warnings',
fixtures.path('es-modules/tla/rejected.mjs'),
]);
assert.match(stderr, /Error: Xyz/);
assert.strictEqual(stdout, '');
assert.strictEqual(code, 1);
});
it('should exit for an unsettled TLA promise and respect explicit exit code via stdin', async () => {
const { code, stderr, stdout } = await spawnPromisified(execPath, [
'--no-warnings',
fixtures.path('es-modules/tla/unresolved-withexitcode.mjs'),
]);
assert.strictEqual(stderr, '');
assert.strictEqual(stdout, '');
assert.strictEqual(code, 42);
});
it('should throw for a rejected TLA promise and ignore explicit exit code via stdin', async () => {
const { code, stderr, stdout } = await spawnPromisified(execPath, [
'--no-warnings',
fixtures.path('es-modules/tla/rejected-withexitcode.mjs'),
]);
assert.match(stderr, /Error: Xyz/);
assert.strictEqual(stdout, '');
assert.strictEqual(code, 1);
});
it('should exit successfully when calling `process.exit()` in `.mjs` file', async () => {
const { code, stderr, stdout } = await spawnPromisified(execPath, [
'--no-warnings',
fixtures.path('es-modules/tla/process-exit.mjs'),
]);
assert.strictEqual(stderr, '');
assert.strictEqual(stdout, '');
assert.strictEqual(code, 0);
});
it('should be unaffected by `process.exit()` in worker thread', async () => {
const { code, stderr, stdout } = await spawnPromisified(execPath, [
fixtures.path('es-modules/tla/unresolved-with-worker-process-exit.mjs'),
]);
assert.match(stderr, /Warning: Detected unsettled top-level await at.+with-worker-process-exit\.mjs:5/);
assert.match(stderr, /await new Promise/);
assert.strictEqual(stdout, '');
assert.strictEqual(code, 13);
});
it('should be unaffected by `process.exit()` in worker thread without warning', async () => {
const { code, stderr, stdout } = await spawnPromisified(execPath, [
'--no-warnings',
fixtures.path('es-modules/tla/unresolved-with-worker-process-exit.mjs'),
]);
assert.strictEqual(stderr, '');
assert.strictEqual(stdout, '');
assert.strictEqual(code, 13);
});
});