Skip to content

Commit

Permalink
test_runner: fix recursive run
Browse files Browse the repository at this point in the history
PR-URL: nodejs#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]>
  • Loading branch information
MoLow authored Apr 4, 2024
1 parent f1f32d8 commit 433bd1b
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 433bd1b

Please sign in to comment.