11cb0ef41Sopenharmony_ciimport { spawnPromisified } from '../common/index.mjs';
21cb0ef41Sopenharmony_ciimport fixtures from '../common/fixtures.js';
31cb0ef41Sopenharmony_ciimport assert from 'node:assert';
41cb0ef41Sopenharmony_ciimport { execPath } from 'node:process';
51cb0ef41Sopenharmony_ciimport { describe, it } from 'node:test';
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst commonArgs = [
91cb0ef41Sopenharmony_ci  '--no-warnings',
101cb0ef41Sopenharmony_ci  '--input-type=module',
111cb0ef41Sopenharmony_ci  '--eval',
121cb0ef41Sopenharmony_ci];
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_cidescribe('ESM: unsettled and rejected promises', { concurrency: true }, () => {
151cb0ef41Sopenharmony_ci  it('should exit for an unsettled TLA promise via --eval', async () => {
161cb0ef41Sopenharmony_ci    const { code, stderr, stdout } = await spawnPromisified(execPath, [
171cb0ef41Sopenharmony_ci      ...commonArgs,
181cb0ef41Sopenharmony_ci      'await new Promise(() => {})',
191cb0ef41Sopenharmony_ci    ]);
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
221cb0ef41Sopenharmony_ci    assert.strictEqual(stdout, '');
231cb0ef41Sopenharmony_ci    assert.strictEqual(code, 13);
241cb0ef41Sopenharmony_ci  });
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  it('should throw for a rejected TLA promise via --eval', async () => {
271cb0ef41Sopenharmony_ci    // Rejected TLA promise, --eval
281cb0ef41Sopenharmony_ci    const { code, stderr, stdout } = await spawnPromisified(execPath, [
291cb0ef41Sopenharmony_ci      ...commonArgs,
301cb0ef41Sopenharmony_ci      'await Promise.reject(new Error("Xyz"))',
311cb0ef41Sopenharmony_ci    ]);
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci    assert.match(stderr, /Error: Xyz/);
341cb0ef41Sopenharmony_ci    assert.strictEqual(stdout, '');
351cb0ef41Sopenharmony_ci    assert.strictEqual(code, 1);
361cb0ef41Sopenharmony_ci  });
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  it('should exit for an unsettled TLA promise and respect explicit exit code via --eval', async () => {
391cb0ef41Sopenharmony_ci    // Rejected TLA promise, --eval
401cb0ef41Sopenharmony_ci    const { code, stderr, stdout } = await spawnPromisified(execPath, [
411cb0ef41Sopenharmony_ci      ...commonArgs,
421cb0ef41Sopenharmony_ci      'process.exitCode = 42;await new Promise(() => {})',
431cb0ef41Sopenharmony_ci    ]);
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
461cb0ef41Sopenharmony_ci    assert.strictEqual(stdout, '');
471cb0ef41Sopenharmony_ci    assert.strictEqual(code, 42);
481cb0ef41Sopenharmony_ci  });
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  it('should throw for a rejected TLA promise and ignore explicit exit code via --eval', async () => {
511cb0ef41Sopenharmony_ci    // Rejected TLA promise, --eval
521cb0ef41Sopenharmony_ci    const { code, stderr, stdout } = await spawnPromisified(execPath, [
531cb0ef41Sopenharmony_ci      ...commonArgs,
541cb0ef41Sopenharmony_ci      'process.exitCode = 42;await Promise.reject(new Error("Xyz"))',
551cb0ef41Sopenharmony_ci    ]);
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci    assert.match(stderr, /Error: Xyz/);
581cb0ef41Sopenharmony_ci    assert.strictEqual(stdout, '');
591cb0ef41Sopenharmony_ci    assert.strictEqual(code, 1);
601cb0ef41Sopenharmony_ci  });
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  it('should exit for an unsettled TLA promise via stdin', async () => {
631cb0ef41Sopenharmony_ci    const { code, stderr, stdout } = await spawnPromisified(execPath, [
641cb0ef41Sopenharmony_ci      '--no-warnings',
651cb0ef41Sopenharmony_ci      fixtures.path('es-modules/tla/unresolved.mjs'),
661cb0ef41Sopenharmony_ci    ]);
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
691cb0ef41Sopenharmony_ci    assert.strictEqual(stdout, '');
701cb0ef41Sopenharmony_ci    assert.strictEqual(code, 13);
711cb0ef41Sopenharmony_ci  });
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci  it('should throw for a rejected TLA promise via stdin', async () => {
741cb0ef41Sopenharmony_ci    const { code, stderr, stdout } = await spawnPromisified(execPath, [
751cb0ef41Sopenharmony_ci      '--no-warnings',
761cb0ef41Sopenharmony_ci      fixtures.path('es-modules/tla/rejected.mjs'),
771cb0ef41Sopenharmony_ci    ]);
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci    assert.match(stderr, /Error: Xyz/);
801cb0ef41Sopenharmony_ci    assert.strictEqual(stdout, '');
811cb0ef41Sopenharmony_ci    assert.strictEqual(code, 1);
821cb0ef41Sopenharmony_ci  });
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci  it('should exit for an unsettled TLA promise and respect explicit exit code via stdin', async () => {
851cb0ef41Sopenharmony_ci    const { code, stderr, stdout } = await spawnPromisified(execPath, [
861cb0ef41Sopenharmony_ci      '--no-warnings',
871cb0ef41Sopenharmony_ci      fixtures.path('es-modules/tla/unresolved-withexitcode.mjs'),
881cb0ef41Sopenharmony_ci    ]);
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
911cb0ef41Sopenharmony_ci    assert.strictEqual(stdout, '');
921cb0ef41Sopenharmony_ci    assert.strictEqual(code, 42);
931cb0ef41Sopenharmony_ci  });
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci  it('should throw for a rejected TLA promise and ignore explicit exit code via stdin', async () => {
961cb0ef41Sopenharmony_ci    const { code, stderr, stdout } = await spawnPromisified(execPath, [
971cb0ef41Sopenharmony_ci      '--no-warnings',
981cb0ef41Sopenharmony_ci      fixtures.path('es-modules/tla/rejected-withexitcode.mjs'),
991cb0ef41Sopenharmony_ci    ]);
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci    assert.match(stderr, /Error: Xyz/);
1021cb0ef41Sopenharmony_ci    assert.strictEqual(stdout, '');
1031cb0ef41Sopenharmony_ci    assert.strictEqual(code, 1);
1041cb0ef41Sopenharmony_ci  });
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci  it('should exit successfully when calling `process.exit()` in `.mjs` file', async () => {
1071cb0ef41Sopenharmony_ci    const { code, stderr, stdout } = await spawnPromisified(execPath, [
1081cb0ef41Sopenharmony_ci      '--no-warnings',
1091cb0ef41Sopenharmony_ci      fixtures.path('es-modules/tla/process-exit.mjs'),
1101cb0ef41Sopenharmony_ci    ]);
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
1131cb0ef41Sopenharmony_ci    assert.strictEqual(stdout, '');
1141cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
1151cb0ef41Sopenharmony_ci  });
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ci  it('should be unaffected by `process.exit()` in worker thread', async () => {
1181cb0ef41Sopenharmony_ci    const { code, stderr, stdout } = await spawnPromisified(execPath, [
1191cb0ef41Sopenharmony_ci      '--no-warnings',
1201cb0ef41Sopenharmony_ci      fixtures.path('es-modules/tla/unresolved-with-worker-process-exit.mjs'),
1211cb0ef41Sopenharmony_ci    ]);
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
1241cb0ef41Sopenharmony_ci    assert.strictEqual(stdout, '');
1251cb0ef41Sopenharmony_ci    assert.strictEqual(code, 13);
1261cb0ef41Sopenharmony_ci  });
1271cb0ef41Sopenharmony_ci});
128