Skip to content

Commit 84de97a

Browse files
authored
test_runner: support forced exit
This commit updates the test runner to allow a forced exit once all known tests have finished running. Fixes: #49925 PR-URL: #52038 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Raz Luvaton <rluvaton@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent 78be0d0 commit 84de97a

15 files changed

+170
-10
lines changed

doc/api/cli.md

+9
Original file line numberDiff line numberDiff line change
@@ -1872,6 +1872,15 @@ added:
18721872
The maximum number of test files that the test runner CLI will execute
18731873
concurrently. The default value is `os.availableParallelism() - 1`.
18741874

1875+
### `--test-force-exit`
1876+
1877+
<!-- YAML
1878+
added: REPLACEME
1879+
-->
1880+
1881+
Configures the test runner to exit the process once all known tests have
1882+
finished executing even if the event loop would otherwise remain active.
1883+
18751884
### `--test-name-pattern`
18761885

18771886
<!-- YAML

doc/api/test.md

+6
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,9 @@ added:
11191119
- v18.9.0
11201120
- v16.19.0
11211121
changes:
1122+
- version: REPLACEME
1123+
pr-url: https://github.com/nodejs/node/pull/52038
1124+
description: Added the `forceExit` option.
11221125
- version:
11231126
- v20.1.0
11241127
- v18.17.0
@@ -1137,6 +1140,9 @@ changes:
11371140
**Default:** `false`.
11381141
* `files`: {Array} An array containing the list of files to run.
11391142
**Default** matching files from [test runner execution model][].
1143+
* `forceExit`: {boolean} Configures the test runner to exit the process once
1144+
all known tests have finished executing even if the event loop would
1145+
otherwise remain active. **Default:** `false`.
11401146
* `inspectPort` {number|Function} Sets inspector port of test child process.
11411147
This can be a number, or a function that takes no arguments and returns a
11421148
number. If a nullish value is provided, each process gets its own port,

doc/node.1

+4
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,10 @@ Starts the Node.js command line test runner.
422422
The maximum number of test files that the test runner CLI will execute
423423
concurrently.
424424
.
425+
.It Fl -test-force-exit
426+
Configures the test runner to exit the process once all known tests have
427+
finished executing even if the event loop would otherwise remain active.
428+
.
425429
.It Fl -test-name-pattern
426430
A regular expression that configures the test runner to only execute tests
427431
whose name matches the provided pattern.

lib/internal/test_runner/harness.js

+1
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ function setup(root) {
195195
suites: 0,
196196
},
197197
shouldColorizeTestFiles: false,
198+
teardown: exitHandler,
198199
};
199200
root.startTime = hrtime();
200201
return root;

lib/internal/test_runner/runner.js

+33-3
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,11 @@ function filterExecArgv(arg, i, arr) {
112112
!ArrayPrototypeSome(kFilterArgValues, (p) => arg === p || (i > 0 && arr[i - 1] === p) || StringPrototypeStartsWith(arg, `${p}=`));
113113
}
114114

115-
function getRunArgs(path, { inspectPort, testNamePatterns, only }) {
115+
function getRunArgs(path, { forceExit, inspectPort, testNamePatterns, only }) {
116116
const argv = ArrayPrototypeFilter(process.execArgv, filterExecArgv);
117+
if (forceExit === true) {
118+
ArrayPrototypePush(argv, '--test-force-exit');
119+
}
117120
if (isUsingInspector()) {
118121
ArrayPrototypePush(argv, `--inspect-port=${getInspectPort(inspectPort)}`);
119122
}
@@ -440,14 +443,33 @@ function run(options = kEmptyObject) {
440443
validateObject(options, 'options');
441444

442445
let { testNamePatterns, shard } = options;
443-
const { concurrency, timeout, signal, files, inspectPort, watch, setup, only } = options;
446+
const {
447+
concurrency,
448+
timeout,
449+
signal,
450+
files,
451+
forceExit,
452+
inspectPort,
453+
watch,
454+
setup,
455+
only,
456+
} = options;
444457

445458
if (files != null) {
446459
validateArray(files, 'options.files');
447460
}
448461
if (watch != null) {
449462
validateBoolean(watch, 'options.watch');
450463
}
464+
if (forceExit != null) {
465+
validateBoolean(forceExit, 'options.forceExit');
466+
467+
if (forceExit && watch) {
468+
throw new ERR_INVALID_ARG_VALUE(
469+
'options.forceExit', watch, 'is not supported with watch mode',
470+
);
471+
}
472+
}
451473
if (only != null) {
452474
validateBoolean(only, 'options.only');
453475
}
@@ -501,7 +523,15 @@ function run(options = kEmptyObject) {
501523

502524
let postRun = () => root.postRun();
503525
let filesWatcher;
504-
const opts = { __proto__: null, root, signal, inspectPort, testNamePatterns, only };
526+
const opts = {
527+
__proto__: null,
528+
root,
529+
signal,
530+
inspectPort,
531+
testNamePatterns,
532+
only,
533+
forceExit,
534+
};
505535
if (watch) {
506536
filesWatcher = watchFiles(testFiles, opts);
507537
postRun = undefined;

lib/internal/test_runner/test.js

+21-7
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,12 @@ const kHookNames = ObjectSeal(['before', 'after', 'beforeEach', 'afterEach']);
7878
const kUnwrapErrors = new SafeSet()
7979
.add(kTestCodeFailure).add(kHookFailure)
8080
.add('uncaughtException').add('unhandledRejection');
81-
const { sourceMaps, testNamePatterns, testOnlyFlag } = parseCommandLine();
81+
const {
82+
forceExit,
83+
sourceMaps,
84+
testNamePatterns,
85+
testOnlyFlag,
86+
} = parseCommandLine();
8287
let kResistStopPropagation;
8388
let findSourceMap;
8489

@@ -748,6 +753,16 @@ class Test extends AsyncResource {
748753
// This helps catch any asynchronous activity that occurs after the tests
749754
// have finished executing.
750755
this.postRun();
756+
} else if (forceExit) {
757+
// This is the root test, and all known tests and hooks have finished
758+
// executing. If the user wants to force exit the process regardless of
759+
// any remaining ref'ed handles, then do that now. It is theoretically
760+
// possible that a ref'ed handle could asynchronously create more tests,
761+
// but the user opted into this behavior.
762+
this.reporter.once('close', () => {
763+
process.exit();
764+
});
765+
this.harness.teardown();
751766
}
752767
}
753768

@@ -798,12 +813,11 @@ class Test extends AsyncResource {
798813
if (this.parent === this.root &&
799814
this.root.activeSubtests === 0 &&
800815
this.root.pendingSubtests.length === 0 &&
801-
this.root.readySubtests.size === 0 &&
802-
this.root.hooks.after.length > 0) {
803-
// This is done so that any global after() hooks are run. At this point
804-
// all of the tests have finished running. However, there might be
805-
// ref'ed handles keeping the event loop alive. This gives the global
806-
// after() hook a chance to clean them up.
816+
this.root.readySubtests.size === 0) {
817+
// At this point all of the tests have finished running. However, there
818+
// might be ref'ed handles keeping the event loop alive. This gives the
819+
// global after() hook a chance to clean them up. The user may also
820+
// want to force the test runner to exit despite ref'ed handles.
807821
this.root.run();
808822
}
809823
} else if (!this.reported) {

lib/internal/test_runner/utils.js

+2
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ function parseCommandLine() {
193193

194194
const isTestRunner = getOptionValue('--test');
195195
const coverage = getOptionValue('--experimental-test-coverage');
196+
const forceExit = getOptionValue('--test-force-exit');
196197
const sourceMaps = getOptionValue('--enable-source-maps');
197198
const isChildProcess = process.env.NODE_TEST_CONTEXT === 'child';
198199
const isChildProcessV8 = process.env.NODE_TEST_CONTEXT === 'child-v8';
@@ -245,6 +246,7 @@ function parseCommandLine() {
245246
__proto__: null,
246247
isTestRunner,
247248
coverage,
249+
forceExit,
248250
sourceMaps,
249251
testOnlyFlag,
250252
testNamePatterns,

src/node_options.cc

+6
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ void EnvironmentOptions::CheckOptions(std::vector<std::string>* errors,
179179
} else if (force_repl) {
180180
errors->push_back("either --watch or --interactive "
181181
"can be used, not both");
182+
} else if (test_runner_force_exit) {
183+
errors->push_back("either --watch or --test-force-exit "
184+
"can be used, not both");
182185
} else if (!test_runner && (argv->size() < 1 || (*argv)[1].empty())) {
183186
errors->push_back("--watch requires specifying a file");
184187
}
@@ -616,6 +619,9 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
616619
AddOption("--test-concurrency",
617620
"specify test runner concurrency",
618621
&EnvironmentOptions::test_runner_concurrency);
622+
AddOption("--test-force-exit",
623+
"force test runner to exit upon completion",
624+
&EnvironmentOptions::test_runner_force_exit);
619625
AddOption("--test-timeout",
620626
"specify test runner timeout",
621627
&EnvironmentOptions::test_runner_timeout);

src/node_options.h

+1
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ class EnvironmentOptions : public Options {
166166
uint64_t test_runner_concurrency = 0;
167167
uint64_t test_runner_timeout = 0;
168168
bool test_runner_coverage = false;
169+
bool test_runner_force_exit = false;
169170
std::vector<std::string> test_name_pattern;
170171
std::vector<std::string> test_reporter;
171172
std::vector<std::string> test_reporter_destination;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Flags: --test-force-exit --test-reporter=spec
2+
'use strict';
3+
const { after, afterEach, before, beforeEach, test } = require('node:test');
4+
5+
before(() => {
6+
console.log('BEFORE');
7+
});
8+
9+
beforeEach(() => {
10+
console.log('BEFORE EACH');
11+
});
12+
13+
after(() => {
14+
console.log('AFTER');
15+
});
16+
17+
afterEach(() => {
18+
console.log('AFTER EACH');
19+
});
20+
21+
test('passes but oops', () => {
22+
setTimeout(() => {
23+
throw new Error('this should not have a chance to be thrown');
24+
}, 1000);
25+
});
26+
27+
test('also passes');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
BEFORE
2+
BEFORE EACH
3+
AFTER EACH
4+
BEFORE EACH
5+
AFTER EACH
6+
AFTER
7+
✔ passes but oops (*ms)
8+
✔ also passes (*ms)
9+
ℹ tests 2
10+
ℹ suites 0
11+
ℹ pass 2
12+
ℹ fail 0
13+
ℹ cancelled 0
14+
ℹ skipped 0
15+
ℹ todo 0
16+
ℹ duration_ms *
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
const { test } = require('node:test');
3+
4+
test('fails and schedules more work', () => {
5+
setTimeout(() => {
6+
throw new Error('this should not have a chance to be thrown');
7+
}, 1000);
8+
9+
throw new Error('fails');
10+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
require('../common');
3+
const { match, doesNotMatch, strictEqual } = require('node:assert');
4+
const { spawnSync } = require('node:child_process');
5+
const fixtures = require('../common/fixtures');
6+
const fixture = fixtures.path('test-runner/throws_sync_and_async.js');
7+
const r = spawnSync(process.execPath, ['--test', '--test-force-exit', fixture]);
8+
9+
strictEqual(r.status, 1);
10+
strictEqual(r.signal, null);
11+
strictEqual(r.stderr.toString(), '');
12+
13+
const stdout = r.stdout.toString();
14+
match(stdout, /error: 'fails'/);
15+
doesNotMatch(stdout, /this should not have a chance to be thrown/);

test/parallel/test-runner-output.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ const tests = [
101101
{ name: 'test-runner/output/global-hooks-with-no-tests.js' },
102102
{ name: 'test-runner/output/before-and-after-each-too-many-listeners.js' },
103103
{ name: 'test-runner/output/before-and-after-each-with-timeout-too-many-listeners.js' },
104+
{ name: 'test-runner/output/force_exit.js', transform: specTransform },
104105
{ name: 'test-runner/output/global_after_should_fail_the_test.js' },
105106
{ name: 'test-runner/output/no_refs.js' },
106107
{ name: 'test-runner/output/no_tests.js' },

test/parallel/test-runner-run.mjs

+18
Original file line numberDiff line numberDiff line change
@@ -513,3 +513,21 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
513513
for await (const _ of stream);
514514
});
515515
});
516+
517+
describe('forceExit', () => {
518+
it('throws for non-boolean values', () => {
519+
[Symbol(), {}, 0, 1, '1', Promise.resolve([])].forEach((forceExit) => {
520+
assert.throws(() => run({ forceExit }), {
521+
code: 'ERR_INVALID_ARG_TYPE',
522+
message: /The "options\.forceExit" property must be of type boolean\./
523+
});
524+
});
525+
});
526+
527+
it('throws if enabled with watch mode', () => {
528+
assert.throws(() => run({ forceExit: true, watch: true }), {
529+
code: 'ERR_INVALID_ARG_VALUE',
530+
message: /The property 'options\.forceExit' is not supported with watch mode\./
531+
});
532+
});
533+
});

0 commit comments

Comments
 (0)