Skip to content

test_runner: allow requiring reporter from node_modules #45916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/internal/modules/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function shouldUseESMLoader(filePath) {
* @returns {any}
* requireOrImport imports a module if the file is an ES module, otherwise it requires it.
*/
function requireOrImport(filePath) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

esmLoader.import‘s second parameter is parentURL, but you haven’t updated that call (on line 45 here) like you have for Module._load. If there were a test where the reporter were ESM (rather than the single new test in this PR, with reporter r as CommonJS) this would have been caught.

Why do we need this utility? Node.js has --import, which accepts either CommonJS or ESM. Can’t --test-reporter be an alias for that, or map to it?

function requireOrImport(filePath, parent = null) {
const useESMLoader = shouldUseESMLoader(filePath);
if (useESMLoader) {
const { esmLoader } = require('internal/process/esm_loader');
Expand All @@ -46,7 +46,7 @@ function requireOrImport(filePath) {
}
const { Module } = require('internal/modules/cjs/loader');

return new Module._load(filePath, null, false);
return new Module._load(filePath, parent, false);
}

module.exports = {
Expand Down
13 changes: 12 additions & 1 deletion lib/internal/test_runner/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,21 @@ const kBuiltinReporters = new SafeMap([
const kDefaultReporter = 'tap';
const kDefaultDestination = 'stdout';

let paerentModule = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let paerentModule = null;
let parentModule = null;

function getParentModule() {
if (paerentModule) {
return paerentModule;
}
const { Module } = require('internal/modules/cjs/loader');
paerentModule = new Module('internal/node:test', null);
paerentModule.paths = Module._nodeModulePaths(process.cwd());
return paerentModule;
}

async function getReportersMap(reporters, destinations) {
return SafePromiseAllReturnArrayLike(reporters, async (name, i) => {
const destination = kBuiltinDestinations.get(destinations[i]) ?? createWriteStream(destinations[i]);
let reporter = await requireOrImport(kBuiltinReporters.get(name) ?? name);
let reporter = await requireOrImport(kBuiltinReporters.get(name) ?? name, getParentModule());

if (reporter?.default) {
reporter = reporter.default;
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/test-runner/node_modules/r/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions test/fixtures/test-runner/node_modules/r/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions test/parallel/test-runner-reporters.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,13 @@ describe('node:test reporters', { concurrency: true }, () => {
assert.strictEqual(child.stdout.toString(), `${filename} {"test:start":5,"test:pass":2,"test:fail":3,"test:plan":3,"test:diagnostic":7}`);
});
});

it('should support a custom reporter from node_modules', async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test for a similar reporter that’s written in ESM.

const child = spawnSync(process.execPath,
['--test', '--test-reporter', 'r', 'reporters.js'],
{ cwd: fixtures.path('test-runner') });
assert.strictEqual(child.stderr.toString(), '');
assert.strictEqual(child.stdout.toString(),
'package: r{"test:start":5,"test:pass":2,"test:fail":3,"test:plan":3,"test:diagnostic":7}');
});
});