Skip to content

Commit

Permalink
process: wait promise resolve before print result
Browse files Browse the repository at this point in the history
  • Loading branch information
H4ad committed Mar 20, 2024
1 parent f9755f6 commit a808430
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 27 deletions.
5 changes: 4 additions & 1 deletion lib/internal/process/execution.js
Expand Up @@ -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)
Expand Down
79 changes: 53 additions & 26 deletions test/parallel/test-cli-print-promise.mjs
Expand Up @@ -15,7 +15,7 @@ describe('--print with a promise', { concurrency: true }, () => {
code: 0,
signal: null,
stderr: '',
stdout: 'Promise { 42 }\n',
stdout: '42\n',
});
});

Expand All @@ -29,65 +29,92 @@ describe('--print with a promise', { concurrency: true }, () => {
code: 0,
signal: null,
stderr: '',
stdout: 'Promise { <pending> }\n',
stdout: '42\n',
});
});

it('should handle promise that never settles', async () => {
it('should error with unhandled rejected promises', async () => {
const result = await spawnPromisified(execPath, [
'--print',
'new Promise(()=>{})',
'Promise.reject(1)',
]);

assert.deepStrictEqual(result, {
code: 0,
signal: null,
stderr: '',
stdout: 'Promise { <pending> }\n',
});
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 output something if process exits before promise settles', async () => {
it('should error when throw inside fn', async () => {
const result = await spawnPromisified(execPath, [
'--print',
'setTimeout(process.exit,100, 0);timers.promises.setTimeout(200)',
'Promise.resolve().then(()=>{throw new Error(10)})',
]);

assert.deepStrictEqual(result, {
code: 0,
signal: null,
stderr: '',
stdout: 'Promise { <pending> }\n',
});
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}`);
});

it('should handle rejected promises', async () => {
it('should handle promise that never settles', async () => {
const result = await spawnPromisified(execPath, [
'--unhandled-rejections=none',
'--print',
'Promise.reject(1)',
'new Promise(()=>{})',
]);

assert.deepStrictEqual(result, {
code: 0,
signal: null,
stderr: '',
stdout: 'Promise { <rejected> 1 }\n',
stdout: '',
});
});

it('should handle promises that reject after one tick', async () => {
it('should output something if process exits before promise settles', async () => {
const result = await spawnPromisified(execPath, [
'--unhandled-rejections=none',
'--print',
'Promise.resolve().then(()=>Promise.reject(1))',
'setTimeout(process.exit,100, 0);timers.promises.setTimeout(200)',
]);

assert.deepStrictEqual(result, {
code: 0,
signal: null,
stderr: '',
stdout: 'Promise { <pending> }\n',
stdout: '',
});
});

describe('when under unhandled-rejections=none', () => {
it('should handle rejected promises', async () => {
const result = await spawnPromisified(execPath, [
'--unhandled-rejections=none',
'--print',
'Promise.reject(1)',
]);

assert.deepStrictEqual(result, {
code: 0,
signal: null,
stderr: '',
stdout: '',
});
});

it('should handle promises that reject after one tick', async () => {
const result = await spawnPromisified(execPath, [
'--unhandled-rejections=none',
'--print',
'Promise.resolve().then(()=>Promise.reject(1))',
]);

assert.deepStrictEqual(result, {
code: 0,
signal: null,
stderr: '',
stdout: '',
});
});
});
});

0 comments on commit a808430

Please sign in to comment.