Skip to content

Commit f26d3af

Browse files
committedApr 3, 2024
test_runner: fix recursive run
1 parent 74343a7 commit f26d3af

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed
 

‎lib/internal/test_runner/runner.js

+2
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,8 @@ function run(options = kEmptyObject) {
519519
root.harness.shouldColorizeTestFiles ||= shouldColorizeTestFiles(root);
520520

521521
if (process.env.NODE_TEST_CONTEXT !== undefined) {
522+
process.emitWarning('node:test run() is being called recursively within a test file. skipping running files.');
523+
root.postRun();
522524
return root.reporter;
523525
}
524526
let testFiles = files ?? createTestFileList();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
const { test, run } = require('node:test');
4+
5+
test('recursive run() calls', async () => {
6+
for await (const event of run({ files: [__filename] }));
7+
});

‎test/parallel/test-runner-run.mjs

+12
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,18 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
522522
// eslint-disable-next-line no-unused-vars
523523
for await (const _ of stream);
524524
});
525+
526+
it('should avoid running recursively', async () => {
527+
const stream = run({ files: [join(testFixtures, 'recursive_run.js')] });
528+
let stderr = '';
529+
stream.on('test:fail', common.mustNotCall());
530+
stream.on('test:pass', common.mustCall(1));
531+
stream.on('test:stderr', (c) => { stderr += c.message; });
532+
533+
// eslint-disable-next-line no-unused-vars
534+
for await (const _ of stream);
535+
assert.match(stderr, /Warning: node:test run\(\) is being called recursively/);
536+
});
525537
});
526538

527539
describe('forceExit', () => {

0 commit comments

Comments
 (0)