• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import { spawnPromisified } from '../common/index.mjs';
2import fixtures from '../common/fixtures.js';
3import assert from 'node:assert';
4import { execPath } from 'node:process';
5import { describe, it } from 'node:test';
6
7
8const commonArgs = [
9  '--no-warnings',
10  '--input-type=module',
11  '--eval',
12];
13
14describe('ESM: unsettled and rejected promises', { concurrency: true }, () => {
15  it('should exit for an unsettled TLA promise via --eval', async () => {
16    const { code, stderr, stdout } = await spawnPromisified(execPath, [
17      ...commonArgs,
18      'await new Promise(() => {})',
19    ]);
20
21    assert.strictEqual(stderr, '');
22    assert.strictEqual(stdout, '');
23    assert.strictEqual(code, 13);
24  });
25
26  it('should throw for a rejected TLA promise via --eval', async () => {
27    // Rejected TLA promise, --eval
28    const { code, stderr, stdout } = await spawnPromisified(execPath, [
29      ...commonArgs,
30      'await Promise.reject(new Error("Xyz"))',
31    ]);
32
33    assert.match(stderr, /Error: Xyz/);
34    assert.strictEqual(stdout, '');
35    assert.strictEqual(code, 1);
36  });
37
38  it('should exit for an unsettled TLA promise and respect explicit exit code via --eval', async () => {
39    // Rejected TLA promise, --eval
40    const { code, stderr, stdout } = await spawnPromisified(execPath, [
41      ...commonArgs,
42      'process.exitCode = 42;await new Promise(() => {})',
43    ]);
44
45    assert.strictEqual(stderr, '');
46    assert.strictEqual(stdout, '');
47    assert.strictEqual(code, 42);
48  });
49
50  it('should throw for a rejected TLA promise and ignore explicit exit code via --eval', async () => {
51    // Rejected TLA promise, --eval
52    const { code, stderr, stdout } = await spawnPromisified(execPath, [
53      ...commonArgs,
54      'process.exitCode = 42;await Promise.reject(new Error("Xyz"))',
55    ]);
56
57    assert.match(stderr, /Error: Xyz/);
58    assert.strictEqual(stdout, '');
59    assert.strictEqual(code, 1);
60  });
61
62  it('should exit for an unsettled TLA promise via stdin', async () => {
63    const { code, stderr, stdout } = await spawnPromisified(execPath, [
64      '--no-warnings',
65      fixtures.path('es-modules/tla/unresolved.mjs'),
66    ]);
67
68    assert.strictEqual(stderr, '');
69    assert.strictEqual(stdout, '');
70    assert.strictEqual(code, 13);
71  });
72
73  it('should throw for a rejected TLA promise via stdin', async () => {
74    const { code, stderr, stdout } = await spawnPromisified(execPath, [
75      '--no-warnings',
76      fixtures.path('es-modules/tla/rejected.mjs'),
77    ]);
78
79    assert.match(stderr, /Error: Xyz/);
80    assert.strictEqual(stdout, '');
81    assert.strictEqual(code, 1);
82  });
83
84  it('should exit for an unsettled TLA promise and respect explicit exit code via stdin', async () => {
85    const { code, stderr, stdout } = await spawnPromisified(execPath, [
86      '--no-warnings',
87      fixtures.path('es-modules/tla/unresolved-withexitcode.mjs'),
88    ]);
89
90    assert.strictEqual(stderr, '');
91    assert.strictEqual(stdout, '');
92    assert.strictEqual(code, 42);
93  });
94
95  it('should throw for a rejected TLA promise and ignore explicit exit code via stdin', async () => {
96    const { code, stderr, stdout } = await spawnPromisified(execPath, [
97      '--no-warnings',
98      fixtures.path('es-modules/tla/rejected-withexitcode.mjs'),
99    ]);
100
101    assert.match(stderr, /Error: Xyz/);
102    assert.strictEqual(stdout, '');
103    assert.strictEqual(code, 1);
104  });
105
106  it('should exit successfully when calling `process.exit()` in `.mjs` file', async () => {
107    const { code, stderr, stdout } = await spawnPromisified(execPath, [
108      '--no-warnings',
109      fixtures.path('es-modules/tla/process-exit.mjs'),
110    ]);
111
112    assert.strictEqual(stderr, '');
113    assert.strictEqual(stdout, '');
114    assert.strictEqual(code, 0);
115  });
116
117  it('should be unaffected by `process.exit()` in worker thread', async () => {
118    const { code, stderr, stdout } = await spawnPromisified(execPath, [
119      '--no-warnings',
120      fixtures.path('es-modules/tla/unresolved-with-worker-process-exit.mjs'),
121    ]);
122
123    assert.strictEqual(stderr, '');
124    assert.strictEqual(stdout, '');
125    assert.strictEqual(code, 13);
126  });
127});
128