Skip to content

Commit

Permalink
Treat the natural Node.js process termination as exit code 0 (#24)
Browse files Browse the repository at this point in the history
Fixes #23
  • Loading branch information
jakobo committed Oct 22, 2022
1 parent a0f032e commit 69881e7
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion fixture-async.js → fixtures/async.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import process from 'node:process';
import exitHook, {asyncExitHook, gracefulExit} from './index.js';
import exitHook, {asyncExitHook, gracefulExit} from '../index.js';

exitHook(() => {
console.log('foo');
Expand Down
5 changes: 5 additions & 0 deletions fixtures/empty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import exitHook from '../index.js';

exitHook(() => {
// https://github.com/sindresorhus/exit-hook/issues/23
});
2 changes: 1 addition & 1 deletion fixture.js → fixtures/sync.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import process from 'node:process';
import exitHook from './index.js';
import exitHook from '../index.js';

exitHook(() => {
console.log('foo');
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function addHook(options) {
isRegistered = true;

// Exit cases that support asynchronous handling
process.once('beforeExit', exit.bind(undefined, true, false, 0));
process.once('beforeExit', exit.bind(undefined, true, false, -128));
process.once('SIGINT', exit.bind(undefined, true, false, 2));
process.once('SIGTERM', exit.bind(undefined, true, false, 15));

Expand Down
15 changes: 12 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,34 @@ import execa from 'execa';
import exitHook, {asyncExitHook} from './index.js';

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

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

test('main-async', async t => {
const {stdout, stderr} = await execa(process.execPath, ['fixture-async.js']);
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-notice', async t => {
const {stdout, stderr} = await execa(process.execPath, ['fixture-async.js'], {
const {stdout, stderr, exitCode} = await execa(process.execPath, ['./fixtures/async.js'], {
env: {
EXIT_HOOK_SYNC: '1',
},
});
t.is(stdout, 'foo\nbar');
t.regex(stderr, /SYNCHRONOUS TERMINATION NOTICE/);
t.is(exitCode, 0);
});

test('listener count', t => {
Expand Down

0 comments on commit 69881e7

Please sign in to comment.