Skip to content

Commit 0f690f0

Browse files
MoLowmarco-ippolito
authored andcommitted
test_runner: fix recursive run
PR-URL: #52322 Reviewed-By: Chemi Atlow <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent 893b5aa commit 0f690f0

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
@@ -541,6 +541,8 @@ function run(options) {
541541
root.harness.shouldColorizeTestFiles ||= shouldColorizeTestFiles(root);
542542

543543
if (process.env.NODE_TEST_CONTEXT !== undefined) {
544+
process.emitWarning('node:test run() is being called recursively within a test file. skipping running files.');
545+
root.postRun();
544546
return root.reporter;
545547
}
546548
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
@@ -513,4 +513,16 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
513513
// eslint-disable-next-line no-unused-vars
514514
for await (const _ of stream);
515515
});
516+
517+
it('should avoid running recursively', async () => {
518+
const stream = run({ files: [join(testFixtures, 'recursive_run.js')] });
519+
let stderr = '';
520+
stream.on('test:fail', common.mustNotCall());
521+
stream.on('test:pass', common.mustCall(1));
522+
stream.on('test:stderr', (c) => { stderr += c.message; });
523+
524+
// eslint-disable-next-line no-unused-vars
525+
for await (const _ of stream);
526+
assert.match(stderr, /Warning: node:test run\(\) is being called recursively/);
527+
});
516528
});

0 commit comments

Comments
 (0)