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: 9 additions & 5 deletions lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const {
ERR_TEST_FAILURE,
},
} = require('internal/errors');
const signalNumbers = internalBinding('constants').os.signals;
const { exitCodes: { kGenericUserError } } = internalBinding('errors');
const { kCancelledByParent, Test, Suite } = require('internal/test_runner/test');
const {
Expand Down Expand Up @@ -285,8 +286,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', sigintHandler);
process.removeListener('SIGTERM', sigtermHandler);
}
};

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

const terminationHandler = async () => {
const terminationHandler = async (signal) => {
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));
}
process.exitCode = 128 + signalNumbers[signal];
await exitHandler(true);
process.exit();
};
const sigintHandler = FunctionPrototypeBind(terminationHandler, null, 'SIGINT');
const sigtermHandler = FunctionPrototypeBind(terminationHandler, null, 'SIGTERM');

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', sigintHandler);
process.on('SIGTERM', sigtermHandler);
}

root.harness.coverage = FunctionPrototypeBind(collectCoverage, null, root, coverage);
Expand Down
14 changes: 7 additions & 7 deletions test/parallel/test-runner-exit-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
const { once } = require('events');
const { finished } = require('stream/promises');

async function runAndKill(file, expectedTestName) {
async function runAndKill(file, expectedTestName, killSignal, expectedCode) {
if (common.isWindows) {
common.printSkipMessage(`signals are not supported in windows, skipping ${file}`);
return;
Expand All @@ -15,17 +15,17 @@
const child = spawn(process.execPath, ['--test', '--test-reporter=tap', file]);
child.stdout.setEncoding('utf8');
child.stdout.on('data', (chunk) => {
if (!stdout.length) child.kill('SIGINT');
if (!stdout.length) child.kill(killSignal);
stdout += chunk;
});
const [code, signal] = await once(child, 'exit');
const [code, exitSignal] = await once(child, 'exit');
await finished(child.stdout);
assert(stdout.startsWith('TAP version 13\n'));
// Verify interrupted test message
assert(stdout.includes(`Interrupted while running: ${expectedTestName}`),
`Expected output to contain interrupted test name`);
assert.strictEqual(signal, null);
assert.strictEqual(code, 1);
assert.strictEqual(exitSignal, null);
assert.strictEqual(code, expectedCode);

Check failure on line 28 in test/parallel/test-runner-exit-code.js

View workflow job for this annotation

GitHub Actions / test-macOS

--- stderr --- node:internal/process/promises:332 triggerUncaughtException(err, true /* fromPromise */); ^ AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: 1 !== 143 at runAndKill (/Users/runner/work/node/node/node/test/parallel/test-runner-exit-code.js:28:10) at process.processTicksAndRejections (node:internal/process/task_queues:104:5) { generatedMessage: true, code: 'ERR_ASSERTION', actual: 1, expected: 143, operator: 'strictEqual', diff: 'simple' } Node.js v26.0.0-pre Command: out/Release/node --test-reporter=./test/common/test-error-reporter.js --test-reporter-destination=stdout /Users/runner/work/node/node/node/test/parallel/test-runner-exit-code.js

Check failure on line 28 in test/parallel/test-runner-exit-code.js

View workflow job for this annotation

GitHub Actions / test-linux (ubuntu-24.04-arm)

--- stderr --- node:internal/process/promises:332 triggerUncaughtException(err, true /* fromPromise */); ^ AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: 1 !== 130 at runAndKill (/home/runner/work/node/node/node/test/parallel/test-runner-exit-code.js:28:10) at process.processTicksAndRejections (node:internal/process/task_queues:104:5) { generatedMessage: true, code: 'ERR_ASSERTION', actual: 1, expected: 130, operator: 'strictEqual', diff: 'simple' } Node.js v26.0.0-pre Command: out/Release/node --test-reporter=./test/common/test-error-reporter.js --test-reporter-destination=stdout /home/runner/work/node/node/node/test/parallel/test-runner-exit-code.js

Check failure on line 28 in test/parallel/test-runner-exit-code.js

View workflow job for this annotation

GitHub Actions / test-linux (ubuntu-24.04)

--- stderr --- node:internal/process/promises:332 triggerUncaughtException(err, true /* fromPromise */); ^ AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: 1 !== 143 at runAndKill (/home/runner/work/node/node/node/test/parallel/test-runner-exit-code.js:28:10) at process.processTicksAndRejections (node:internal/process/task_queues:104:5) { generatedMessage: true, code: 'ERR_ASSERTION', actual: 1, expected: 143, operator: 'strictEqual', diff: 'simple' } Node.js v26.0.0-pre Command: out/Release/node --test-reporter=./test/common/test-error-reporter.js --test-reporter-destination=stdout /home/runner/work/node/node/node/test/parallel/test-runner-exit-code.js

Check failure on line 28 in test/parallel/test-runner-exit-code.js

View workflow job for this annotation

GitHub Actions / aarch64-linux: with shared libraries

--- stderr --- node:internal/process/promises:332 triggerUncaughtException(err, true /* fromPromise */); ^ AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: 1 !== 130 at runAndKill (/home/runner/work/_temp/node-v26.0.0-nightly2026-03-25a441e75678-slim/test/parallel/test-runner-exit-code.js:28:10) at process.processTicksAndRejections (node:internal/process/task_queues:104:5) { generatedMessage: true, code: 'ERR_ASSERTION', actual: 1, expected: 130, operator: 'strictEqual', diff: 'simple' } Node.js v26.0.0-pre Command: out/Release/node --test-reporter=./test/common/test-error-reporter.js --test-reporter-destination=stdout /home/runner/work/_temp/node-v26.0.0-nightly2026-03-25a441e75678-slim/test/parallel/test-runner-exit-code.js

Check failure on line 28 in test/parallel/test-runner-exit-code.js

View workflow job for this annotation

GitHub Actions / x86_64-darwin: with shared libraries

--- stderr --- node:internal/process/promises:332 triggerUncaughtException(err, true /* fromPromise */); ^ AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: 1 !== 143 at runAndKill (/Users/runner/work/_temp/node-v26.0.0-nightly2026-03-25a441e75678-slim/test/parallel/test-runner-exit-code.js:28:10) at process.processTicksAndRejections (node:internal/process/task_queues:104:5) { generatedMessage: true, code: 'ERR_ASSERTION', actual: 1, expected: 143, operator: 'strictEqual', diff: 'simple' } Node.js v26.0.0-pre Command: out/Release/node --test-reporter=./test/common/test-error-reporter.js --test-reporter-destination=stdout /Users/runner/work/_temp/node-v26.0.0-nightly2026-03-25a441e75678-slim/test/parallel/test-runner-exit-code.js

Check failure on line 28 in test/parallel/test-runner-exit-code.js

View workflow job for this annotation

GitHub Actions / aarch64-darwin: with shared libraries

--- stderr --- node:internal/process/promises:332 triggerUncaughtException(err, true /* fromPromise */); ^ AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: 1 !== 130 at runAndKill (/Users/runner/work/_temp/node-v26.0.0-nightly2026-03-25a441e75678-slim/test/parallel/test-runner-exit-code.js:28:10) at process.processTicksAndRejections (node:internal/process/task_queues:104:5) { generatedMessage: true, code: 'ERR_ASSERTION', actual: 1, expected: 130, operator: 'strictEqual', diff: 'simple' } Node.js v26.0.0-pre Command: out/Release/node --test-reporter=./test/common/test-error-reporter.js --test-reporter-destination=stdout /Users/runner/work/_temp/node-v26.0.0-nightly2026-03-25a441e75678-slim/test/parallel/test-runner-exit-code.js

Check failure on line 28 in test/parallel/test-runner-exit-code.js

View workflow job for this annotation

GitHub Actions / x86_64-linux: with shared libraries

--- stderr --- node:internal/process/promises:332 triggerUncaughtException(err, true /* fromPromise */); ^ AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: 1 !== 130 at runAndKill (/home/runner/work/_temp/node-v26.0.0-nightly2026-03-25a441e75678-slim/test/parallel/test-runner-exit-code.js:28:10) at process.processTicksAndRejections (node:internal/process/task_queues:104:5) { generatedMessage: true, code: 'ERR_ASSERTION', actual: 1, expected: 130, operator: 'strictEqual', diff: 'simple' } Node.js v26.0.0-pre Command: out/Release/node --test-reporter=./test/common/test-error-reporter.js --test-reporter-destination=stdout /home/runner/work/_temp/node-v26.0.0-nightly2026-03-25a441e75678-slim/test/parallel/test-runner-exit-code.js
}

if (process.argv[2] === 'child') {
Expand Down Expand Up @@ -82,6 +82,6 @@
// because the parent runner only knows about file-level tests
const neverEndingSync = fixtures.path('test-runner', 'never_ending_sync.js');
const neverEndingAsync = fixtures.path('test-runner', 'never_ending_async.js');
runAndKill(neverEndingSync, neverEndingSync).then(common.mustCall());
runAndKill(neverEndingAsync, neverEndingAsync).then(common.mustCall());
runAndKill(neverEndingSync, neverEndingSync, 'SIGINT', 130).then(common.mustCall());
runAndKill(neverEndingAsync, neverEndingAsync, 'SIGTERM', 143).then(common.mustCall());
}
Loading