diff --git a/lib/internal/test_runner/utils.js b/lib/internal/test_runner/utils.js index d5d8f219a60a23..2e016a350b571e 100644 --- a/lib/internal/test_runner/utils.js +++ b/lib/internal/test_runner/utils.js @@ -104,7 +104,7 @@ async function getReportersMap(reporters, destinations) { const destination = kBuiltinDestinations.get(destinations[i]) ?? createWriteStream(destinations[i]); // Load the test reporter passed to --test-reporter - let reporterPath = kBuiltinReporters.get(name) ?? name; + const reporterPath = kBuiltinReporters.get(name) ?? name; const { esmLoader } = require('internal/process/esm_loader'); const { pathToFileURL } = require('internal/url'); const file = isAbsolute(reporterPath) ? pathToFileURL(reporterPath).href : reporterPath; diff --git a/test/fixtures/test-runner/node_modules/r-esm/index.mjs b/test/fixtures/test-runner/node_modules/r-esm/index.mjs new file mode 100644 index 00000000000000..494d54b483a314 --- /dev/null +++ b/test/fixtures/test-runner/node_modules/r-esm/index.mjs @@ -0,0 +1,8 @@ +export default async function * customReporter(source) { + const counters = {}; + for await (const event of source) { + counters[event.type] = (counters[event.type] ?? 0) + 1; + } + yield "package: r-esm"; + yield JSON.stringify(counters); +}; diff --git a/test/fixtures/test-runner/node_modules/r-esm/package.json b/test/fixtures/test-runner/node_modules/r-esm/package.json new file mode 100644 index 00000000000000..953b9b83a1a766 --- /dev/null +++ b/test/fixtures/test-runner/node_modules/r-esm/package.json @@ -0,0 +1,4 @@ +{ + "name": "r-esm", + "main": "index.mjs" +} diff --git a/test/fixtures/test-runner/node_modules/r/index.js b/test/fixtures/test-runner/node_modules/r/index.js new file mode 100644 index 00000000000000..463d6c04fd0892 --- /dev/null +++ b/test/fixtures/test-runner/node_modules/r/index.js @@ -0,0 +1,8 @@ +module.exports = async function * customReporter(source) { + const counters = {}; + for await (const event of source) { + counters[event.type] = (counters[event.type] ?? 0) + 1; + } + yield "package: r"; + yield JSON.stringify(counters); +}; diff --git a/test/fixtures/test-runner/node_modules/r/package.json b/test/fixtures/test-runner/node_modules/r/package.json new file mode 100644 index 00000000000000..72508166d19647 --- /dev/null +++ b/test/fixtures/test-runner/node_modules/r/package.json @@ -0,0 +1,4 @@ +{ + "name": "r", + "main": "index.js" +} diff --git a/test/parallel/test-runner-reporters.js b/test/parallel/test-runner-reporters.js index 74cae3401e2843..a6e4be7b93b192 100644 --- a/test/parallel/test-runner-reporters.js +++ b/test/parallel/test-runner-reporters.js @@ -92,4 +92,22 @@ 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 () => { + 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}'); + }); + + it('should support a custom ESM reporter from node_modules', async () => { + const child = spawnSync(process.execPath, + ['--test', '--test-reporter', 'r-esm', 'reporters.js'], + { cwd: fixtures.path('test-runner') }); + assert.strictEqual(child.stderr.toString(), ''); + assert.strictEqual(child.stdout.toString(), + 'package: r-esm{"test:start":5,"test:pass":2,"test:fail":3,"test:plan":3,"test:diagnostic":7}'); + }); });