| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 4d50db1 commit 403df21
10 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -470,7 +470,7 @@ The following built-reporters are supported: | |||
| 470 | 470 | The `spec` reporter outputs the test results in a human-readable format. | |
| 471 | 471 | ||
| 472 | 472 | * `dot` | |
| 473 | - The `dot` reporter outputs the test results in a comact format, | ||
| 473 | + The `dot` reporter outputs the test results in a compact format, | ||
| 474 | 474 | where each passing test is represented by a `.`, | |
| 475 | 475 | and each failing test is represented by a `X`. | |
| 476 | 476 | ||
@@ -591,6 +591,9 @@ module.exports = async function * customReporter(source) { | |||
| 591 | 591 | }; | |
| 592 | 592 | ``` | |
| 593 | 593 | ||
| 594 | + The value provided to `--test-reporter` should be a string like one used in an | ||
| 595 | + `import()` in JavaScript code. | ||
| 596 | + | ||
| 594 | 597 | ### Multiple reporters | |
| 595 | 598 | ||
| 596 | 599 | The [`--test-reporter`][] flag can be specified multiple times to report test | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,12 +2,12 @@ | |||
| 2 | 2 | ||
| 3 | 3 | const { | |
| 4 | 4 | ObjectCreate, | |
| 5 | + StringPrototypeEndsWith, | ||
| 5 | 6 | } = primordials; | |
| 6 | 7 | const CJSLoader = require('internal/modules/cjs/loader'); | |
| 7 | - const { Module, toRealPath } = CJSLoader; | ||
| 8 | + const { Module, toRealPath, readPackageScope } = CJSLoader; | ||
| 8 | 9 | const { getOptionValue } = require('internal/options'); | |
| 9 | 10 | const path = require('path'); | |
| 10 | - const { shouldUseESMLoader } = require('internal/modules/utils'); | ||
| 11 | 11 | const { | |
| 12 | 12 | handleProcessExit, | |
| 13 | 13 | } = require('internal/modules/esm/handle_process_exit'); | |
@@ -27,6 +27,27 @@ function resolveMainPath(main) { | |||
| 27 | 27 | return mainPath; | |
| 28 | 28 | } | |
| 29 | 29 | ||
| 30 | + function shouldUseESMLoader(mainPath) { | ||
| 31 | + /** | ||
| 32 | + * @type {string[]} userLoaders A list of custom loaders registered by the user | ||
| 33 | + * (or an empty list when none have been registered). | ||
| 34 | + */ | ||
| 35 | + const userLoaders = getOptionValue('--experimental-loader'); | ||
| 36 | + if (userLoaders.length > 0) | ||
| 37 | + return true; | ||
| 38 | + const esModuleSpecifierResolution = | ||
| 39 | + getOptionValue('--experimental-specifier-resolution'); | ||
| 40 | + if (esModuleSpecifierResolution === 'node') | ||
| 41 | + return true; | ||
| 42 | + // Determine the module format of the main | ||
| 43 | + if (mainPath && StringPrototypeEndsWith(mainPath, '.mjs')) | ||
| 44 | + return true; | ||
| 45 | + if (!mainPath || StringPrototypeEndsWith(mainPath, '.cjs')) | ||
| 46 | + return false; | ||
| 47 | + const pkg = readPackageScope(mainPath); | ||
| 48 | + return pkg && pkg.data.type === 'module'; | ||
| 49 | + } | ||
| 50 | + | ||
| 30 | 51 | function runMainESM(mainPath) { | |
| 31 | 52 | const { loadESM } = require('internal/process/esm_loader'); | |
| 32 | 53 | const { pathToFileURL } = require('internal/url'); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,7 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | const { | |
| 3 | 3 | ArrayPrototypePush, | |
| 4 | + ObjectCreate, | ||
| 4 | 5 | ObjectGetOwnPropertyDescriptor, | |
| 5 | 6 | SafePromiseAllReturnArrayLike, | |
| 6 | 7 | RegExp, | |
@@ -9,9 +10,9 @@ const { | |||
| 9 | 10 | } = primordials; | |
| 10 | 11 | const { basename } = require('path'); | |
| 11 | 12 | const { createWriteStream } = require('fs'); | |
| 13 | + const { pathToFileURL } = require('internal/url'); | ||
| 12 | 14 | const { createDeferredPromise } = require('internal/util'); | |
| 13 | 15 | const { getOptionValue } = require('internal/options'); | |
| 14 | - const { requireOrImport } = require('internal/modules/utils'); | ||
| 15 | 16 | ||
| 16 | 17 | const { | |
| 17 | 18 | codes: { | |
@@ -103,7 +104,17 @@ const kDefaultDestination = 'stdout'; | |||
| 103 | 104 | async function getReportersMap(reporters, destinations) { | |
| 104 | 105 | return SafePromiseAllReturnArrayLike(reporters, async (name, i) => { | |
| 105 | 106 | const destination = kBuiltinDestinations.get(destinations[i]) ?? createWriteStream(destinations[i]); | |
| 106 | - let reporter = await requireOrImport(kBuiltinReporters.get(name) ?? name); | ||
| 107 | + | ||
| 108 | + // Load the test reporter passed to --test-reporter | ||
| 109 | + const reporterSpecifier = kBuiltinReporters.get(name) ?? name; | ||
| 110 | + let parentURL; | ||
| 111 | + try { | ||
| 112 | + parentURL = pathToFileURL(process.cwd() + '/').href; | ||
| 113 | + } catch { | ||
| 114 | + parentURL = 'file:///'; | ||
| 115 | + } | ||
| 116 | + const { esmLoader } = require('internal/process/esm_loader'); | ||
| 117 | + let reporter = await esmLoader.import(reporterSpecifier, parentURL, ObjectCreate(null)); | ||
| 107 | 118 | ||
| 108 | 119 | if (reporter?.default) { | |
| 109 | 120 | reporter = reporter.default; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -75,7 +75,6 @@ const expectedModules = new Set([ | |||
| 75 | 75 | 'NativeModule internal/mime', | |
| 76 | 76 | 'NativeModule internal/modules/cjs/helpers', | |
| 77 | 77 | 'NativeModule internal/modules/cjs/loader', | |
| 78 | - 'NativeModule internal/modules/utils', | ||
| 79 | 78 | 'NativeModule internal/modules/esm/assert', | |
| 80 | 79 | 'NativeModule internal/modules/esm/create_dynamic_module', | |
| 81 | 80 | 'NativeModule internal/modules/esm/fetch_module', | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -86,10 +86,32 @@ describe('node:test reporters', { concurrency: true }, () => { | |||
| 86 | 86 | it(`should support a '${ext}' file as a custom reporter`, async () => { | |
| 87 | 87 | const filename = `custom.${ext}`; | |
| 88 | 88 | const child = spawnSync(process.execPath, | |
| 89 | - ['--test', '--test-reporter', fixtures.path('test-runner/custom_reporters/', filename), | ||
| 89 | + ['--test', '--test-reporter', fixtures.fileURL('test-runner/custom_reporters/', filename), | ||
| 90 | 90 | testFile]); | |
| 91 | 91 | assert.strictEqual(child.stderr.toString(), ''); | |
| 92 | 92 | assert.strictEqual(child.stdout.toString(), `${filename} {"test:start":5,"test:pass":2,"test:fail":3,"test:plan":3,"test:diagnostic":7}`); | |
| 93 | 93 | }); | |
| 94 | 94 | }); | |
| 95 | + | ||
| 96 | + it('should support a custom reporter from node_modules', async () => { | ||
| 97 | + const child = spawnSync(process.execPath, | ||
| 98 | + ['--test', '--test-reporter', 'reporter-cjs', 'reporters.js'], | ||
| 99 | + { cwd: fixtures.path('test-runner') }); | ||
| 100 | + assert.strictEqual(child.stderr.toString(), ''); | ||
| 101 | + assert.match( | ||
| 102 | + child.stdout.toString(), | ||
| 103 | + /^package: reporter-cjs{"test:start":5,"test:pass":2,"test:fail":3,"test:plan":3,"test:diagnostic":\d+}$/, | ||
| 104 | + ); | ||
| 105 | + }); | ||
| 106 | + | ||
| 107 | + it('should support a custom ESM reporter from node_modules', async () => { | ||
| 108 | + const child = spawnSync(process.execPath, | ||
| 109 | + ['--test', '--test-reporter', 'reporter-esm', 'reporters.js'], | ||
| 110 | + { cwd: fixtures.path('test-runner') }); | ||
| 111 | + assert.strictEqual(child.stderr.toString(), ''); | ||
| 112 | + assert.match( | ||
| 113 | + child.stdout.toString(), | ||
| 114 | + /^package: reporter-esm{"test:start":5,"test:pass":2,"test:fail":3,"test:plan":3,"test:diagnostic":\d+}$/, | ||
| 115 | + ); | ||
| 116 | + }); | ||
| 95 | 117 | }); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments