From 02b90820bb5e29a8679c97f2f9738bc639920f6b Mon Sep 17 00:00:00 2001 From: Pascal Jufer Date: Mon, 4 Jul 2022 09:44:04 +0200 Subject: [PATCH] Enhance tests (#335) --- src/concurrently.spec.ts | 16 ++++++++++------ src/concurrently.ts | 2 +- src/flow-control/log-timings.spec.ts | 10 ++-------- src/flow-control/log-timings.ts | 10 +++++----- 4 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/concurrently.spec.ts b/src/concurrently.spec.ts index 6eba78a4..29977807 100644 --- a/src/concurrently.spec.ts +++ b/src/concurrently.spec.ts @@ -3,9 +3,8 @@ import { concurrently, ConcurrentlyCommandInput, ConcurrentlyOptions } from './c import { createFakeProcess, FakeCommand } from './fixtures/fake-command'; import { FlowController } from './flow-control/flow-controller'; import { Logger } from './logger'; -import { OutputWriter } from './output-writer'; - -jest.mock('./output-writer'); +import { Writable } from 'stream'; +import { createMockInstance } from 'jest-create-mock-instance'; let spawn: SpawnCommand; let kill: KillProcess; @@ -48,10 +47,15 @@ it('spawns all commands', () => { expect(spawn).toHaveBeenCalledWith('kill', expect.objectContaining({})); }); -it('output writer is created if logger is passed in options', () => { +it('log output is passed to output stream if logger is specified in options', () => { const logger = new Logger({ hide: [] }); - create(['foo'], { logger }); - expect(OutputWriter).toHaveBeenCalledTimes(1); + const outputStream = createMockInstance(Writable); + create(['foo'], { logger, outputStream }); + logger.log('foo', 'bar'); + + expect(outputStream.write).toHaveBeenCalledTimes(2); + expect(outputStream.write).toHaveBeenCalledWith('foo'); + expect(outputStream.write).toHaveBeenCalledWith('bar'); }); it('spawns commands up to configured limit at once', () => { diff --git a/src/concurrently.ts b/src/concurrently.ts index 8cff159e..e190bde8 100644 --- a/src/concurrently.ts +++ b/src/concurrently.ts @@ -176,7 +176,7 @@ export function concurrently( ); commands = handleResult.commands; - if (options.logger) { + if (options.logger && options.outputStream) { const outputWriter = new OutputWriter({ outputStream: options.outputStream, group: options.group, diff --git a/src/flow-control/log-timings.spec.ts b/src/flow-control/log-timings.spec.ts index 6a80a915..677f614c 100644 --- a/src/flow-control/log-timings.spec.ts +++ b/src/flow-control/log-timings.spec.ts @@ -109,20 +109,14 @@ it('does not log timings summary if there was an error', () => { }); it('logs the sorted timings summary when all processes close successfully', () => { - jest.spyOn(controller, 'printExitInfoTimingTable'); controller.handle(commands); commands[0].close.next(command0ExitInfo); commands[1].close.next(command1ExitInfo); + expect(logger.logGlobalEvent).toHaveBeenCalledTimes(1); + expect(logger.logGlobalEvent).toHaveBeenCalledWith('Timings:'); expect(logger.logTable).toHaveBeenCalledTimes(1); - - // un-sorted ie by finish order - expect(controller.printExitInfoTimingTable).toHaveBeenCalledWith([ - command0ExitInfo, - command1ExitInfo, - ]); - // sorted by duration expect(logger.logTable).toHaveBeenCalledWith([ LogTimings.mapCloseEventToTimingInfo(command1ExitInfo), diff --git a/src/flow-control/log-timings.ts b/src/flow-control/log-timings.ts index 59884eb7..5e32eebb 100644 --- a/src/flow-control/log-timings.ts +++ b/src/flow-control/log-timings.ts @@ -51,15 +51,15 @@ export class LogTimings implements FlowController { this.timestampFormat = timestampFormat; } - printExitInfoTimingTable(exitInfos: CloseEvent[]) { + private printExitInfoTimingTable(exitInfos: CloseEvent[]) { const exitInfoTable = _(exitInfos) .sortBy(({ timings }) => timings.durationSeconds) .reverse() .map(LogTimings.mapCloseEventToTimingInfo) .value(); - this.logger?.logGlobalEvent('Timings:'); - this.logger?.logTable(exitInfoTable); + this.logger.logGlobalEvent('Timings:'); + this.logger.logTable(exitInfoTable); return exitInfos; } @@ -73,14 +73,14 @@ export class LogTimings implements FlowController { command.timer.subscribe(({ startDate, endDate }) => { if (!endDate) { const formattedStartDate = formatDate(startDate, this.timestampFormat); - this.logger?.logCommandEvent( + this.logger.logCommandEvent( `${command.command} started at ${formattedStartDate}`, command ); } else { const durationMs = endDate.getTime() - startDate.getTime(); const formattedEndDate = formatDate(endDate, this.timestampFormat); - this.logger?.logCommandEvent( + this.logger.logCommandEvent( `${ command.command } stopped at ${formattedEndDate} after ${durationMs.toLocaleString()}ms`,