Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions fixtures/async-exit-code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import process from 'node:process';
import exitHook, {asyncExitHook, gracefulExit} from '../index.js';

exitHook(() => {
console.log('foo');
});

exitHook(() => {
console.log('bar');
});

const unsubscribe = exitHook(() => {
console.log('baz');
});

unsubscribe();

asyncExitHook(
async () => {
await new Promise(resolve => {
setTimeout(() => {
resolve();
}, 100);
});

console.log('quux');
},
{
wait: 200,
},
);

process.exitCode = 1;

if (process.env.EXIT_HOOK_SYNC === '1') {
process.exit(); // eslint-disable-line unicorn/no-process-exit
}

gracefulExit();
7 changes: 7 additions & 0 deletions fixtures/empty-exit-code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import process from 'node:process';
import exitHook from '../index.js';

process.exitCode = 1;
exitHook(() => {
// https://github.com/sindresorhus/exit-hook/issues/23
});
16 changes: 16 additions & 0 deletions fixtures/signal-exit-code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import process from 'node:process';
import exitHook, {asyncExitHook} from '../index.js';

process.exitCode = 1;

exitHook(signal => {
console.log(signal);
});

asyncExitHook(async signal => {
console.log(signal);
}, {
wait: 200,
});

setInterval(() => {}, 1e9);
20 changes: 20 additions & 0 deletions fixtures/sync-exit-code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import process from 'node:process';
import exitHook from '../index.js';

exitHook(() => {
console.log('foo');
});

exitHook(() => {
console.log('bar');
});

const unsubscribe = exitHook(() => {
console.log('baz');
});

unsubscribe();

process.exitCode = 1;

process.exit(); // eslint-disable-line unicorn/no-process-exit
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ async function exit(shouldManuallyExit, isSynchronous, signal) {
].join(' '));
}

const exitCode = 128 + signal;
let exitCode = 0;

// A non-graceful signal should be preserved over process.exitCode
if (signal > 0) {
exitCode = 128 + signal;
// Respect process.exitCode for graceful exits
} else if (typeof process.exitCode === 'number' || typeof process.exitCode === 'string') {
exitCode = process.exitCode;
}

const done = (force = false) => {
if (force === true || shouldManuallyExit === true) {
Expand Down
80 changes: 73 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,52 @@ test('main', async t => {
t.is(exitCode, 0);
});

test('main with exitCode', async t => {
try {
await execa(process.execPath, ['./fixtures/sync-exit-code.js']);
t.fail();
} catch ({stdout, stderr, exitCode}) {
t.is(stdout, 'foo\nbar');
t.is(stderr, '');
t.is(exitCode, 1);
}
});

test('main-empty', async t => {
const {stderr, exitCode} = await execa(process.execPath, ['./fixtures/empty.js']);
t.is(stderr, '');
t.is(exitCode, 0);
});

test('main-empty with exitCode', async t => {
try {
await execa(process.execPath, ['./fixtures/empty-exit-code.js']);
t.fail();
} catch ({stdout, stderr, exitCode}) {
t.is(stdout, '');
t.is(stderr, '');
t.is(exitCode, 1);
}
});

test('main-async', async t => {
const {stdout, stderr, exitCode} = await execa(process.execPath, ['./fixtures/async.js']);
t.is(stdout, 'foo\nbar\nquux');
t.is(stderr, '');
t.is(exitCode, 0);
});

test('main-async with exitCode', async t => {
try {
await execa(process.execPath, ['./fixtures/async-exit-code.js']);
t.fail();
} catch ({stdout, stderr, exitCode}) {
t.is(stdout, 'foo\nbar\nquux');
t.is(stderr, '');
t.is(exitCode, 1);
}
});

test('main-async-notice', async t => {
const {stdout, stderr, exitCode} = await execa(process.execPath, ['./fixtures/async.js'], {
env: {
Expand All @@ -34,25 +67,42 @@ test('main-async-notice', async t => {
t.is(exitCode, 0);
});

test('main-async-notice with exitCode', async t => {
try {
await execa(process.execPath, ['./fixtures/async-exit-code.js'], {
env: {
EXIT_HOOK_SYNC: '1',
},
});
t.fail();
} catch ({stdout, stderr, exitCode}) {
t.is(stdout, 'foo\nbar');
t.regex(stderr, /SYNCHRONOUS TERMINATION NOTICE/);
t.is(exitCode, 1);
}
});

test('listener count', t => {
t.is(process.listenerCount('exit'), 0);
// This function is used as on node20+ flushSync is added internally to the exit handler of nodejs
const exitListenerCount = () => process.listeners('exit').filter(fn => fn.name !== 'flushSync').length;
t.is(exitListenerCount(), 0);

const unsubscribe1 = exitHook(() => {});
const unsubscribe2 = exitHook(() => {});
t.is(process.listenerCount('exit'), 1);
t.is(exitListenerCount(), 1);

// Remove all listeners
unsubscribe1();
unsubscribe2();
t.is(process.listenerCount('exit'), 1);
t.is(exitListenerCount(), 1);

// Re-add listener
const unsubscribe3 = exitHook(() => {});
t.is(process.listenerCount('exit'), 1);
t.is(exitListenerCount(), 1);

// Remove again
unsubscribe3();
t.is(process.listenerCount('exit'), 1);
t.is(exitListenerCount(), 1);

// Add async style listener
const unsubscribe4 = asyncExitHook(
Expand All @@ -61,11 +111,11 @@ test('listener count', t => {
wait: 100,
},
);
t.is(process.listenerCount('exit'), 1);
t.is(exitListenerCount(), 1);

// Remove again
unsubscribe4();
t.is(process.listenerCount('exit'), 1);
t.is(exitListenerCount(), 1);
});

test('type enforcing', t => {
Expand Down Expand Up @@ -115,4 +165,20 @@ for (const [signal, exitCode] of signalTests) {
t.is(error.stdout, `${exitCode}\n${exitCode}`);
}
});

test(`${signal} causes process.exitCode to be ignored`, async t => {
const subprocess = execa(process.execPath, ['./fixtures/signal-exit-code.js']);

setTimeout(() => {
subprocess.kill(signal);
}, 1000);

try {
await subprocess;
} catch (error) {
t.is(error.exitCode, exitCode);
t.is(error.stderr, '');
t.is(error.stdout, `${exitCode}\n${exitCode}`);
}
});
}