Skip to content
Open
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
14 changes: 7 additions & 7 deletions lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const {
ERR_TEST_FAILURE,
},
} = require('internal/errors');
const { exitCodes: { kGenericUserError } } = internalBinding('errors');
const { exitCodes: { kGenericUserError, kSigInt, kSigTerm } } = internalBinding('errors');
const { kCancelledByParent, Test, Suite } = require('internal/test_runner/test');
const {
parseCommandLine,
Expand Down Expand Up @@ -285,8 +285,8 @@ function setupProcessState(root, globalOptions) {
process.removeListener('unhandledRejection', rejectionHandler);
process.removeListener('beforeExit', exitHandler);
if (globalOptions.isTestRunner) {
process.removeListener('SIGINT', terminationHandler);
process.removeListener('SIGTERM', terminationHandler);
process.removeListener('SIGINT', () => terminationHandler(kSigInt));
process.removeListener('SIGTERM', () => terminationHandler(kSigTerm));
}
};

Expand All @@ -310,24 +310,24 @@ function setupProcessState(root, globalOptions) {
return running;
};

const terminationHandler = async () => {
const terminationHandler = async (exitCode) => {
const runningTests = findRunningTests(root);
if (runningTests.length > 0) {
root.reporter.interrupted(runningTests);
// Allow the reporter stream to process the interrupted event
await new Promise((resolve) => setImmediate(resolve));
}
await exitHandler(true);
process.exit();
process.exit(exitCode);
};

process.on('uncaughtException', exceptionHandler);
process.on('unhandledRejection', rejectionHandler);
process.on('beforeExit', exitHandler);
// TODO(MoLow): Make it configurable to hook when isTestRunner === false.
if (globalOptions.isTestRunner) {
process.on('SIGINT', terminationHandler);
process.on('SIGTERM', terminationHandler);
process.on('SIGINT', () => terminationHandler(kSigInt));
process.on('SIGTERM', () => terminationHandler(kSigTerm));
}

root.harness.coverage = FunctionPrototypeBind(collectCoverage, null, root, coverage);
Expand Down
4 changes: 3 additions & 1 deletion src/node_exit_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ namespace node {
/* typically the exit codes are 128 + signal number. We also exit with */ \
/* certain error codes directly for legacy reasons. Here we define those */ \
/* that are used to normalize the exit code on Windows. */ \
V(Abort, 134)
V(Abort, 134) \
V(SigInt, 130) \
V(SigTerm, 143)

// TODO(joyeecheung): expose this to user land when the codes are stable.
// The underlying type should be an int, or we can get undefined behavior when
Expand Down