Skip to content

Commit ddf7518

Browse files
MoLowtargos
authored andcommitted
test_runner: cancel on termination
PR-URL: #43549 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Nitzan Uziely <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent ef174ea commit ddf7518

7 files changed

+46
-10
lines changed

lib/internal/test_runner/harness.js

+18-5
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,14 @@ function setup(root) {
7575
const rejectionHandler =
7676
createProcessEventHandler('unhandledRejection', root);
7777

78-
process.on('uncaughtException', exceptionHandler);
79-
process.on('unhandledRejection', rejectionHandler);
80-
process.on('beforeExit', () => {
78+
const exitHandler = () => {
8179
root.postRun();
8280

8381
let passCount = 0;
8482
let failCount = 0;
8583
let skipCount = 0;
8684
let todoCount = 0;
85+
let cancelledCount = 0;
8786

8887
for (let i = 0; i < root.subtests.length; i++) {
8988
const test = root.subtests[i];
@@ -94,6 +93,8 @@ function setup(root) {
9493
skipCount++;
9594
} else if (test.isTodo) {
9695
todoCount++;
96+
} else if (test.cancelled) {
97+
cancelledCount++;
9798
} else if (!test.passed) {
9899
failCount++;
99100
} else {
@@ -110,6 +111,7 @@ function setup(root) {
110111
root.reporter.diagnostic(root.indent, `tests ${root.subtests.length}`);
111112
root.reporter.diagnostic(root.indent, `pass ${passCount}`);
112113
root.reporter.diagnostic(root.indent, `fail ${failCount}`);
114+
root.reporter.diagnostic(root.indent, `cancelled ${cancelledCount}`);
113115
root.reporter.diagnostic(root.indent, `skipped ${skipCount}`);
114116
root.reporter.diagnostic(root.indent, `todo ${todoCount}`);
115117
root.reporter.diagnostic(root.indent, `duration_ms ${process.uptime()}`);
@@ -119,10 +121,21 @@ function setup(root) {
119121
process.removeListener('unhandledRejection', rejectionHandler);
120122
process.removeListener('uncaughtException', exceptionHandler);
121123

122-
if (failCount > 0) {
124+
if (failCount > 0 || cancelledCount > 0) {
123125
process.exitCode = 1;
124126
}
125-
});
127+
};
128+
129+
const terminationHandler = () => {
130+
exitHandler();
131+
process.exit();
132+
};
133+
134+
process.on('uncaughtException', exceptionHandler);
135+
process.on('unhandledRejection', rejectionHandler);
136+
process.on('beforeExit', exitHandler);
137+
process.on('SIGINT', terminationHandler);
138+
process.on('SIGTERM', terminationHandler);
126139

127140
root.reporter.pipe(process.stdout);
128141
root.reporter.version();

test/message/test_runner_desctibe_it.out

+1
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ not ok 54 - invalid subtest fail
509509
# tests 54
510510
# pass 23
511511
# fail 17
512+
# cancelled 0
512513
# skipped 9
513514
# todo 5
514515
# duration_ms *

test/message/test_runner_no_refs.out

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ not ok 1 - does not keep event loop alive
2323
1..1
2424
# tests 1
2525
# pass 0
26-
# fail 1
26+
# fail 0
27+
# cancelled 1
2728
# skipped 0
2829
# todo 0
2930
# duration_ms *

test/message/test_runner_only_tests.out

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ ok 11 - only = true, with subtests
120120
# tests 11
121121
# pass 1
122122
# fail 0
123+
# cancelled 0
123124
# skipped 10
124125
# todo 0
125126
# duration_ms *

test/message/test_runner_output.out

+1
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@ not ok 57 - invalid subtest fail
582582
# tests 57
583583
# pass 24
584584
# fail 18
585+
# cancelled 0
585586
# skipped 10
586587
# todo 5
587588
# duration_ms *

test/message/test_runner_unresolved_promise.out

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ not ok 3 - fail
2727
1..3
2828
# tests 3
2929
# pass 1
30-
# fail 2
30+
# fail 0
31+
# cancelled 2
3132
# skipped 0
3233
# todo 0
3334
# duration_ms *
+21-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33
const assert = require('assert');
44
const { spawnSync } = require('child_process');
5+
const { setTimeout } = require('timers/promises');
56

67
if (process.argv[2] === 'child') {
78
const test = require('node:test');
@@ -10,12 +11,18 @@ if (process.argv[2] === 'child') {
1011
test('passing test', () => {
1112
assert.strictEqual(true, true);
1213
});
13-
} else {
14+
} else if (process.argv[3] === 'fail') {
1415
assert.strictEqual(process.argv[3], 'fail');
1516
test('failing test', () => {
1617
assert.strictEqual(true, false);
1718
});
18-
}
19+
} else if (process.argv[3] === 'never_ends') {
20+
assert.strictEqual(process.argv[3], 'never_ends');
21+
test('never ending test', () => {
22+
return setTimeout(100_000_000);
23+
});
24+
process.kill(process.pid, 'SIGINT');
25+
} else assert.fail('unreachable');
1926
} else {
2027
let child = spawnSync(process.execPath, [__filename, 'child', 'pass']);
2128
assert.strictEqual(child.status, 0);
@@ -24,4 +31,15 @@ if (process.argv[2] === 'child') {
2431
child = spawnSync(process.execPath, [__filename, 'child', 'fail']);
2532
assert.strictEqual(child.status, 1);
2633
assert.strictEqual(child.signal, null);
34+
35+
child = spawnSync(process.execPath, [__filename, 'child', 'never_ends']);
36+
assert.strictEqual(child.status, 1);
37+
assert.strictEqual(child.signal, null);
38+
if (common.isWindows) {
39+
common.printSkipMessage('signals are not supported in windows');
40+
} else {
41+
const stdout = child.stdout.toString();
42+
assert.match(stdout, /not ok 1 - never ending test/);
43+
assert.match(stdout, /# cancelled 1/);
44+
}
2745
}

0 commit comments

Comments
 (0)