Skip to content

Commit

Permalink
test_runner: fix recursive run
Browse files Browse the repository at this point in the history
PR-URL: #52322
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
MoLow authored and pull[bot] committed Apr 16, 2024
1 parent ee7a312 commit 1166e51
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/internal/test_runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,8 @@ function run(options = kEmptyObject) {
root.harness.shouldColorizeTestFiles ||= shouldColorizeTestFiles(root);

if (process.env.NODE_TEST_CONTEXT !== undefined) {
process.emitWarning('node:test run() is being called recursively within a test file. skipping running files.');
root.postRun();
return root.reporter;
}
let testFiles = files ?? createTestFileList();
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/test-runner/recursive_run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

const { test, run } = require('node:test');

test('recursive run() calls', async () => {
for await (const event of run({ files: [__filename] }));
});
12 changes: 12 additions & 0 deletions test/parallel/test-runner-run.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,18 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
});

it('should avoid running recursively', async () => {
const stream = run({ files: [join(testFixtures, 'recursive_run.js')] });
let stderr = '';
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', common.mustCall(1));
stream.on('test:stderr', (c) => { stderr += c.message; });

// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
assert.match(stderr, /Warning: node:test run\(\) is being called recursively/);
});
});

describe('forceExit', () => {
Expand Down

0 comments on commit 1166e51

Please sign in to comment.