Files
node/test/parallel/test-runner-exit-code.js
T
Matteo Collina 7215ea428c test_runner: show interrupted test on SIGINT
When the test runner process is killed with SIGINT (Ctrl+C), display
which test was running at the time of interruption. This makes it
easier to identify tests that hang or take too long.

- Add `test:interrupted` event emitted when SIGINT is received
- Add `interrupted()` method to TestsStream
- Handle the event in both TAP and spec reporters
- TAP outputs: `# Interrupted while running: <test>`
- Spec outputs with yellow header and warning symbol
- Use setImmediate to allow reporter stream to flush before exit

With process isolation (default), shows the file path since the
parent runner only knows about file-level tests.
With --test-isolation=none, shows the actual test name.

PR-URL: https://github.com/nodejs/node/pull/61676
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
2026-02-14 16:29:52 +00:00

80 lines
2.8 KiB
JavaScript

'use strict';
const common = require('../common');
const fixtures = require('../common/fixtures');
const assert = require('assert');
const { spawnSync, spawn } = require('child_process');
const { once } = require('events');
const { finished } = require('stream/promises');
async function runAndKill(file, expectedTestName) {
if (common.isWindows) {
common.printSkipMessage(`signals are not supported in windows, skipping ${file}`);
return;
}
let stdout = '';
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');
stdout += chunk;
});
const [code, signal] = 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);
}
if (process.argv[2] === 'child') {
const test = require('node:test');
if (process.argv[3] === 'pass') {
test('passing test', () => {
assert.strictEqual(true, true);
});
} else if (process.argv[3] === 'fail') {
assert.strictEqual(process.argv[3], 'fail');
test('failing test', () => {
assert.strictEqual(true, false);
});
} else assert.fail('unreachable');
} else {
let child = spawnSync(process.execPath, [__filename, 'child', 'pass']);
assert.strictEqual(child.status, 0);
assert.strictEqual(child.signal, null);
child = spawnSync(process.execPath, [
'--test',
fixtures.path('test-runner', 'default-behavior', 'subdir', 'subdir_test.js'),
]);
assert.strictEqual(child.status, 0);
assert.strictEqual(child.signal, null);
child = spawnSync(process.execPath, [
'--test',
fixtures.path('test-runner', 'todo_exit_code.js'),
]);
assert.strictEqual(child.status, 0);
assert.strictEqual(child.signal, null);
const stdout = child.stdout.toString();
assert.match(stdout, /tests 4/);
assert.match(stdout, /pass 0/);
assert.match(stdout, /fail 0/);
assert.match(stdout, /todo 4/);
child = spawnSync(process.execPath, [__filename, 'child', 'fail']);
assert.strictEqual(child.status, 1);
assert.strictEqual(child.signal, null);
// With process isolation (default), the test name shown is the file path
// 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());
}