From a8b21fdc904ed68cb742f4ffd690c83b5865b400 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Mon, 25 Mar 2024 13:28:54 +0100 Subject: [PATCH] process: wait for `'exit'` before printing result MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Vinícius Lourenço Co-authored-by: Antoine du Hamel PR-URL: https://github.com/nodejs/node/pull/52172 Refs: https://github.com/nodejs/node/pull/52077 Reviewed-By: Moshe Atlow Reviewed-By: Luigi Pinca Reviewed-By: Vinícius Lourenço Claro Cardoso Reviewed-By: Chemi Atlow Reviewed-By: Matteo Collina Reviewed-By: Michaël Zasso --- lib/internal/process/execution.js | 5 +- test/parallel/test-cli-print-promise.mjs | 65 ++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/lib/internal/process/execution.js b/lib/internal/process/execution.js index e69add7394e60f..ee28dcf206fc30 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', () => { + log(result); + }); } if (origModule !== undefined) diff --git a/test/parallel/test-cli-print-promise.mjs b/test/parallel/test-cli-print-promise.mjs index 4ed3f65858a571..39b1f41ff98db2 100644 --- a/test/parallel/test-cli-print-promise.mjs +++ b/test/parallel/test-cli-print-promise.mjs @@ -29,7 +29,7 @@ describe('--print with a promise', { concurrency: !process.env.TEST_PARALLEL }, code: 0, signal: null, stderr: '', - stdout: 'Promise { }\n', + stdout: 'Promise { 42 }\n', }); }); @@ -50,7 +50,7 @@ describe('--print with a promise', { concurrency: !process.env.TEST_PARALLEL }, 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)', + 'setTimeout(process.exit, 100, 0);timers.promises.setTimeout(200)', ]); assert.deepStrictEqual(result, { @@ -61,6 +61,20 @@ describe('--print with a promise', { concurrency: !process.env.TEST_PARALLEL }, }); }); + 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 { }\n', + }); + }); + it('should handle rejected promises', async () => { const result = await spawnPromisified(execPath, [ '--unhandled-rejections=none', @@ -87,7 +101,52 @@ describe('--print with a promise', { concurrency: !process.env.TEST_PARALLEL }, code: 0, signal: null, stderr: '', - stdout: 'Promise { }\n', + stdout: 'Promise { 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', }); }); });