Skip to content

Commit

Permalink
fixup! src: fix process exit listeners not receiving unsettled tla codes
Browse files Browse the repository at this point in the history
split tests
  • Loading branch information
dario-piotrowicz committed Feb 4, 2025
1 parent 80a0041 commit 549873d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 32 deletions.
51 changes: 28 additions & 23 deletions test/es-module/test-esm-tla-unfinished.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -72,31 +72,24 @@ describe('ESM: unsettled and rejected promises', { concurrency: !process.env.TES
});

it('should exit for an unsettled TLA promise with warning', async () => {
const { code, stderr } = await spawnPromisified(execPath, [
const { code, stderr, stdout } = await spawnPromisified(execPath, [
fixtures.path('es-modules/tla/unresolved.mjs'),
]);

assert.match(stderr, /Warning: Detected unsettled top-level await at.+unresolved\.mjs:5/);
assert.match(stderr, /Warning: Detected unsettled top-level await at.+unresolved\.mjs:1/);
assert.match(stderr, /await new Promise/);
assert.strictEqual(code, 13);
});

it('the process exit event should provide the correct code for an unsettled TLA promise', async () => {
const { code, stdout } = await spawnPromisified(execPath, [
fixtures.path('es-modules/tla/unresolved.mjs'),
]);

assert.strictEqual(stdout, 'the exit listener received code: 13\n');
assert.strictEqual(stdout, '');
assert.strictEqual(code, 13);
});

it('should exit for an unsettled TLA promise without warning', async () => {
const { code, stderr } = await spawnPromisified(execPath, [
const { code, stderr, stdout } = await spawnPromisified(execPath, [
'--no-warnings',
fixtures.path('es-modules/tla/unresolved.mjs'),
]);

assert.strictEqual(stderr, '');
assert.strictEqual(stdout, '');
assert.strictEqual(code, 13);
});

Expand All @@ -112,22 +105,13 @@ describe('ESM: unsettled and rejected promises', { concurrency: !process.env.TES
});

it('should exit for an unsettled TLA promise and respect explicit exit code via stdin', async () => {
const { code, stderr } = await spawnPromisified(execPath, [
const { code, stderr, stdout } = await spawnPromisified(execPath, [
'--no-warnings',
fixtures.path('es-modules/tla/unresolved-withexitcode.mjs'),
]);

assert.strictEqual(stderr, '');
assert.strictEqual(code, 42);
});

it('should exit for an unsettled TLA promise and respect explicit exit code in process exit event', async () => {
const { code, stdout } = await spawnPromisified(execPath, [
'--no-warnings',
fixtures.path('es-modules/tla/unresolved-withexitcode.mjs'),
]);

assert.strictEqual(stdout, 'the exit listener received code: 42\n');
assert.strictEqual(stdout, '');
assert.strictEqual(code, 42);
});

Expand Down Expand Up @@ -174,4 +158,25 @@ describe('ESM: unsettled and rejected promises', { concurrency: !process.env.TES
assert.strictEqual(stdout, '');
assert.strictEqual(code, 13);
});

describe('with exit listener', () => {
it('the process exit event should provide the correct code', async () => {
const { code, stdout } = await spawnPromisified(execPath, [
fixtures.path('es-modules/tla/unresolved-with-listener.mjs'),
]);

assert.strictEqual(stdout, 'the exit listener received code: 13\n');
assert.strictEqual(code, 13);
});

it('should exit for an unsettled TLA promise and respect explicit exit code in process exit event', async () => {
const { code, stdout } = await spawnPromisified(execPath, [
'--no-warnings',
fixtures.path('es-modules/tla/unresolved-withexitcode-and-listener.mjs'),
]);

assert.strictEqual(stdout, 'the exit listener received code: 42\n');
assert.strictEqual(code, 42);
});
});
});
5 changes: 5 additions & 0 deletions test/fixtures/es-modules/tla/unresolved-with-listener.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
process.on('exit', (exitCode) => {
console.log(`the exit listener received code: ${exitCode}`);
})

await new Promise(() => {});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
process.on('exit', (exitCode) => {
console.log(`the exit listener received code: ${exitCode}`);
});

process.exitCode = 42;

await new Promise(() => {});
5 changes: 0 additions & 5 deletions test/fixtures/es-modules/tla/unresolved-withexitcode.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
process.on('exit', (exitCode) => {
console.log(`the exit listener received code: ${exitCode}`);
});

process.exitCode = 42;

await new Promise(() => {});
4 changes: 0 additions & 4 deletions test/fixtures/es-modules/tla/unresolved.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
process.on('exit', (exitCode) => {
console.log(`the exit listener received code: ${exitCode}`);
})

await new Promise(() => {});

0 comments on commit 549873d

Please sign in to comment.