From 57de87d035a3a49e021f14b6d4dab6fe97c20f02 Mon Sep 17 00:00:00 2001 From: Geoffrey Booth Date: Thu, 22 Dec 2022 13:40:32 -0800 Subject: [PATCH] fix: move test reporter loading Move the logic for handling --test-reporter out of the general module loader and into the test_runner subsystem. PR-URL: https://github.com/nodejs/node/pull/45923 Reviewed-By: Moshe Atlow Reviewed-By: Benjamin Gruenbaum Reviewed-By: Colin Ihrig Reviewed-By: Jacob Smith Reviewed-By: Antoine du Hamel (cherry picked from commit 12c0571c8fece32d274eaf0ae197c0eb1948fe11) --- README.md | 5 ++++- lib/internal/test_runner/utils.js | 7 +++++-- test/common/fixtures.js | 9 +++++++-- test/parallel/test-runner-reporters.js | 8 +++++--- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index d422753..6c93b27 100644 --- a/README.md +++ b/README.md @@ -447,7 +447,7 @@ The following built-reporters are supported: The `spec` reporter outputs the test results in a human-readable format. * `dot` - The `dot` reporter outputs the test results in a comact format, + The `dot` reporter outputs the test results in a compact format, where each passing test is represented by a `.`, and each failing test is represented by a `X`. @@ -564,6 +564,9 @@ module.exports = async function * customReporter(source) { }; ``` +The value provided to `--test-reporter` should be a string like one used in an +`import()` in JavaScript code, or a value provided for [`--import`][]. + ### Multiple reporters The [`--test-reporter`][] flag can be specified multiple times to report test diff --git a/lib/internal/test_runner/utils.js b/lib/internal/test_runner/utils.js index 65d6bc0..f3603f4 100644 --- a/lib/internal/test_runner/utils.js +++ b/lib/internal/test_runner/utils.js @@ -1,4 +1,4 @@ -// https://github.com/nodejs/node/blob/a1b27b25bb01aadd3fd2714e4b136db11b7eb85a/lib/internal/test_runner/utils.js +// https://github.com/nodejs/node/blob/12c0571c8fece32d274eaf0ae197c0eb1948fe11/lib/internal/test_runner/utils.js 'use strict' const { ArrayPrototypePush, @@ -103,7 +103,10 @@ const kDefaultDestination = 'stdout' async function getReportersMap (reporters, destinations) { return SafePromiseAllReturnArrayLike(reporters, async (name, i) => { const destination = kBuiltinDestinations.get(destinations[i]) ?? createWriteStream(destinations[i]) - let reporter = await import(kBuiltinReporters.get(name) ?? name) + + // Load the test reporter passed to --test-reporter + const reporterSpecifier = kBuiltinReporters.get(name) ?? name + let reporter = await import(reporterSpecifier) if (reporter?.default) { reporter = reporter.default diff --git a/test/common/fixtures.js b/test/common/fixtures.js index abe59cd..829cfc7 100644 --- a/test/common/fixtures.js +++ b/test/common/fixtures.js @@ -1,14 +1,19 @@ -// https://github.com/nodejs/node/blob/1aab13cad9c800f4121c1d35b554b78c1b17bdbd/test/common/fixtures.js +// https://github.com/nodejs/node/blob/12c0571c8fece32d274eaf0ae197c0eb1948fe11/test/common/fixtures.js 'use strict' const path = require('path') +const { pathToFileURL } = require('url') const fixturesDir = path.join(__dirname, '..', 'fixtures') function fixturesPath (...args) { return path.join(fixturesDir, ...args) } +function fixturesFileURL (...args) { + return pathToFileURL(fixturesPath(...args)) +} module.exports = { - path: fixturesPath + path: fixturesPath, + fileURL: fixturesFileURL } diff --git a/test/parallel/test-runner-reporters.js b/test/parallel/test-runner-reporters.js index 29984d7..fdec121 100644 --- a/test/parallel/test-runner-reporters.js +++ b/test/parallel/test-runner-reporters.js @@ -1,4 +1,4 @@ -// https://github.com/nodejs/node/blob/a1b27b25bb01aadd3fd2714e4b136db11b7eb85a/test/parallel/test-runner-reporters.js +// https://github.com/nodejs/node/blob/12c0571c8fece32d274eaf0ae197c0eb1948fe11/test/parallel/test-runner-reporters.js 'use strict' require('../common') @@ -91,10 +91,12 @@ describe('node:test reporters', { concurrency: true }, () => { it(`should support a '${ext}' file as a custom reporter`, async () => { const filename = `custom.${ext}` const child = spawnSync(process.execPath, - ['--test', '--test-reporter', fixtures.path('test-runner/custom_reporters/', filename), + ['--test', '--test-reporter', fixtures.fileURL('test-runner/custom_reporters/', filename), testFile]) assert.strictEqual(child.stderr.toString(), '') - assert.strictEqual(child.stdout.toString(), `${filename} {"test:start":5,"test:pass":2,"test:fail":3,"test:plan":3,"test:diagnostic":7}`) + const stdout = child.stdout.toString() + assert.match(stdout, /{"test:start":5,"test:pass":2,"test:fail":3,"test:plan":3,"test:diagnostic":\d+}$/) + assert.strictEqual(stdout.slice(0, filename.length + 2), `${filename} {`) }) }) })