From dbcf465a478d618c7c49f0e637066947d51b5a15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Louren=C3=A7o?= Date: Wed, 13 Mar 2024 21:49:27 -0300 Subject: [PATCH] process: wait promise resolve before print result --- lib/internal/process/execution.js | 5 ++- test/parallel/test-cli-print-promise.mjs | 41 +++++++++++++++++++----- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/lib/internal/process/execution.js b/lib/internal/process/execution.js index e69add7394e60f..8680f51f2a4d22 100644 --- a/lib/internal/process/execution.js +++ b/lib/internal/process/execution.js @@ -119,7 +119,10 @@ function evalScript(name, body, breakFirstLine, print, shouldLoadESM = false) { }); if (print) { const { log } = require('internal/console/global'); - log(result); + + process.on('exit', async () => { + log(await result); + }); } if (origModule !== undefined) diff --git a/test/parallel/test-cli-print-promise.mjs b/test/parallel/test-cli-print-promise.mjs index 3f95630693035e..d57e88c9754de2 100644 --- a/test/parallel/test-cli-print-promise.mjs +++ b/test/parallel/test-cli-print-promise.mjs @@ -15,7 +15,7 @@ describe('--print with a promise', { concurrency: true }, () => { code: 0, signal: null, stderr: '', - stdout: 'Promise { 42 }\n', + stdout: '42\n', }); }); @@ -29,7 +29,7 @@ describe('--print with a promise', { concurrency: true }, () => { code: 0, signal: null, stderr: '', - stdout: 'Promise { }\n', + stdout: '42\n', }); }); @@ -43,7 +43,7 @@ describe('--print with a promise', { concurrency: true }, () => { code: 0, signal: null, stderr: '', - stdout: 'Promise { }\n', + stdout: '', }); }); @@ -57,11 +57,11 @@ describe('--print with a promise', { concurrency: true }, () => { code: 0, signal: null, stderr: '', - stdout: 'Promise { }\n', + stdout: '', }); }); - it('should handle rejected promises', async () => { + it('should handle rejected promises with unhandled-rejections=none', async () => { const result = await spawnPromisified(execPath, [ '--unhandled-rejections=none', '--print', @@ -72,11 +72,11 @@ describe('--print with a promise', { concurrency: true }, () => { code: 0, signal: null, stderr: '', - stdout: 'Promise { 1 }\n', + stdout: '', }); }); - it('should handle promises that reject after one tick', async () => { + it('should handle promises that reject after one tick with unhandled-rejections=none', async () => { const result = await spawnPromisified(execPath, [ '--unhandled-rejections=none', '--print', @@ -87,7 +87,32 @@ describe('--print with a promise', { concurrency: true }, () => { code: 0, signal: null, stderr: '', - stdout: 'Promise { }\n', + stdout: '', }); }); + + it('should error with unhandled rejected promises', async () => { + const result = await spawnPromisified(execPath, [ + '--print', + 'Promise.reject(1)', + ]); + + assert.strictEqual(result.code, 1); + assert.strictEqual(result.signal, null); + assert.strictEqual(result.stdout, ''); + assert.ok(result.stderr.includes('ERR_UNHANDLED_REJECTION'), 'Not found ERR_UNHANDLED_REJECTION'); + }); + + it('should error when throw inside fn', async () => { + const result = await spawnPromisified(execPath, [ + '--print', + 'Promise.resolve().then(()=>{throw new Error(10)})', + ]); + + assert.strictEqual(result.code, 1); + assert.strictEqual(result.signal, null); + assert.strictEqual(result.stdout, ''); + assert.ok(result.stderr.includes('throw new Error(10)'), `Found: ${result.stderr}`); + assert.ok(result.stderr.includes('Error: 10'), `Found: ${result.stderr}`); + }); });