| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -23,6 +23,7 @@ const { | |||
| 23 | 23 | parseCommandLine, | |
| 24 | 24 | reporterScope, | |
| 25 | 25 | setupTestReporters, | |
| 26 | + shouldColorizeTestFiles, | ||
| 26 | 27 | } = require('internal/test_runner/utils'); | |
| 27 | 28 | const { bigint: hrtime } = process.hrtime; | |
| 28 | 29 | ||
@@ -205,7 +206,8 @@ function getGlobalRoot() { | |||
| 205 | 206 | process.exitCode = kGenericUserError; | |
| 206 | 207 | } | |
| 207 | 208 | }); | |
| 208 | - reportersSetup = setupTestReporters(globalRoot); | ||
| 209 | + reportersSetup = setupTestReporters(globalRoot.reporter); | ||
| 210 | + globalRoot.harness.shouldColorizeTestFiles ||= shouldColorizeTestFiles(globalRoot); | ||
| 209 | 211 | } | |
| 210 | 212 | return globalRoot; | |
| 211 | 213 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -70,6 +70,7 @@ const { | |||
| 70 | 70 | countCompletedTest, | |
| 71 | 71 | doesPathMatchFilter, | |
| 72 | 72 | isSupportedFileType, | |
| 73 | + shouldColorizeTestFiles, | ||
| 73 | 74 | } = require('internal/test_runner/utils'); | |
| 74 | 75 | const { basename, join, resolve } = require('path'); | |
| 75 | 76 | const { once } = require('events'); | |
@@ -531,6 +532,8 @@ function run(options) { | |||
| 531 | 532 | } | |
| 532 | 533 | ||
| 533 | 534 | const root = createTestTree({ __proto__: null, concurrency, timeout, signal }); | |
| 535 | + root.harness.shouldColorizeTestFiles ||= shouldColorizeTestFiles(root); | ||
| 536 | + | ||
| 534 | 537 | if (process.env.NODE_TEST_CONTEXT !== undefined) { | |
| 535 | 538 | return root.reporter; | |
| 536 | 539 | } | |
@@ -556,7 +559,7 @@ function run(options) { | |||
| 556 | 559 | }); | |
| 557 | 560 | }; | |
| 558 | 561 | ||
| 559 | - PromisePrototypeThen(PromisePrototypeThen(PromiseResolve(setup?.(root)), runFiles), postRun); | ||
| 562 | + PromisePrototypeThen(PromisePrototypeThen(PromiseResolve(setup?.(root.reporter)), runFiles), postRun); | ||
| 560 | 563 | ||
| 561 | 564 | return root.reporter; | |
| 562 | 565 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,6 +5,7 @@ const { | |||
| 5 | 5 | ArrayPrototypeFlatMap, | |
| 6 | 6 | ArrayPrototypePush, | |
| 7 | 7 | ArrayPrototypeReduce, | |
| 8 | + ArrayPrototypeSome, | ||
| 8 | 9 | ObjectGetOwnPropertyDescriptor, | |
| 9 | 10 | MathFloor, | |
| 10 | 11 | MathMax, | |
@@ -134,10 +135,18 @@ function tryBuiltinReporter(name) { | |||
| 134 | 135 | return require(builtinPath); | |
| 135 | 136 | } | |
| 136 | 137 | ||
| 137 | - async function getReportersMap(reporters, destinations, rootTest) { | ||
| 138 | + function shouldColorizeTestFiles(rootTest) { | ||
| 139 | + // This function assumes only built-in destinations (stdout/stderr) supports coloring | ||
| 140 | + const { reporters, destinations } = parseCommandLine(); | ||
| 141 | + return ArrayPrototypeSome(reporters, (_, index) => { | ||
| 142 | + const destination = kBuiltinDestinations.get(destinations[index]); | ||
| 143 | + return destination && shouldColorize(destination); | ||
| 144 | + }); | ||
| 145 | + } | ||
| 146 | + | ||
| 147 | + async function getReportersMap(reporters, destinations) { | ||
| 138 | 148 | return SafePromiseAllReturnArrayLike(reporters, async (name, i) => { | |
| 139 | 149 | const destination = kBuiltinDestinations.get(destinations[i]) ?? createWriteStream(destinations[i]); | |
| 140 | - rootTest.harness.shouldColorizeTestFiles ||= shouldColorize(destination); | ||
| 141 | 150 | ||
| 142 | 151 | // Load the test reporter passed to --test-reporter | |
| 143 | 152 | let reporter = tryBuiltinReporter(name); | |
@@ -172,12 +181,12 @@ async function getReportersMap(reporters, destinations, rootTest) { | |||
| 172 | 181 | } | |
| 173 | 182 | ||
| 174 | 183 | const reporterScope = new AsyncResource('TestReporterScope'); | |
| 175 | - const setupTestReporters = reporterScope.bind(async (rootTest) => { | ||
| 184 | + const setupTestReporters = reporterScope.bind(async (rootReporter) => { | ||
| 176 | 185 | const { reporters, destinations } = parseCommandLine(); | |
| 177 | - const reportersMap = await getReportersMap(reporters, destinations, rootTest); | ||
| 186 | + const reportersMap = await getReportersMap(reporters, destinations); | ||
| 178 | 187 | for (let i = 0; i < reportersMap.length; i++) { | |
| 179 | 188 | const { reporter, destination } = reportersMap[i]; | |
| 180 | - compose(rootTest.reporter, reporter).pipe(destination); | ||
| 189 | + compose(rootReporter, reporter).pipe(destination); | ||
| 181 | 190 | } | |
| 182 | 191 | }); | |
| 183 | 192 | ||
@@ -428,5 +437,6 @@ module.exports = { | |||
| 428 | 437 | parseCommandLine, | |
| 429 | 438 | reporterScope, | |
| 430 | 439 | setupTestReporters, | |
| 440 | + shouldColorizeTestFiles, | ||
| 431 | 441 | getCoverageReport, | |
| 432 | 442 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -155,4 +155,23 @@ describe('node:test reporters', { concurrency: true }, () => { | |||
| 155 | 155 | assert.strictEqual(child.stdout.toString(), 'Going to throw an error\n'); | |
| 156 | 156 | assert.match(child.stderr.toString(), /Emitted 'error' event on Duplex instance/); | |
| 157 | 157 | }); | |
| 158 | + | ||
| 159 | + it('should support stdout as a destination with spec reporter', async () => { | ||
| 160 | + process.env.FORCE_COLOR = '1'; | ||
| 161 | + const file = tmpdir.resolve(`${tmpFiles++}.txt`); | ||
| 162 | + const child = spawnSync(process.execPath, | ||
| 163 | + ['--test', '--test-reporter', 'spec', '--test-reporter-destination', file, testFile]); | ||
| 164 | + assert.strictEqual(child.stderr.toString(), ''); | ||
| 165 | + assert.strictEqual(child.stdout.toString(), ''); | ||
| 166 | + const fileConent = fs.readFileSync(file, 'utf8'); | ||
| 167 | + assert.match(fileConent, /▶ nested/); | ||
| 168 | + assert.match(fileConent, /✔ ok/); | ||
| 169 | + assert.match(fileConent, /✖ failing/); | ||
| 170 | + assert.match(fileConent, /ℹ tests 4/); | ||
| 171 | + assert.match(fileConent, /ℹ pass 2/); | ||
| 172 | + assert.match(fileConent, /ℹ fail 2/); | ||
| 173 | + assert.match(fileConent, /ℹ cancelled 0/); | ||
| 174 | + assert.match(fileConent, /ℹ skipped 0/); | ||
| 175 | + assert.match(fileConent, /ℹ todo 0/); | ||
| 176 | + }); | ||
| 158 | 177 | }); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -459,6 +459,21 @@ describe('require(\'node:test\').run', { concurrency: true }, () => { | |||
| 459 | 459 | }); | |
| 460 | 460 | }); | |
| 461 | 461 | ||
| 462 | + describe('validation', () => { | ||
| 463 | + it('should pass instance of stream to setup', async () => { | ||
| 464 | + const stream = run({ | ||
| 465 | + files: [join(testFixtures, 'default-behavior/test/random.cjs')], | ||
| 466 | + setup: common.mustCall((root) => { | ||
| 467 | + assert.strictEqual(root.constructor.name, 'TestsStream'); | ||
| 468 | + }), | ||
| 469 | + }); | ||
| 470 | + stream.on('test:fail', common.mustNotCall()); | ||
| 471 | + stream.on('test:pass', common.mustCall()); | ||
| 472 | + // eslint-disable-next-line no-unused-vars | ||
| 473 | + for await (const _ of stream); | ||
| 474 | + }); | ||
| 475 | + }); | ||
| 476 | + | ||
| 462 | 477 | it('should run with no files', async () => { | |
| 463 | 478 | const stream = run({ | |
| 464 | 479 | files: undefined | |
| Back | FazBrowse Home | New Git URL |
0 commit comments