Skip to content

Commit

Permalink
Enhance tests (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
paescuj committed Jul 4, 2022
1 parent cc05702 commit 02b9082
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
16 changes: 10 additions & 6 deletions src/concurrently.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/concurrently.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 2 additions & 8 deletions src/flow-control/log-timings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
10 changes: 5 additions & 5 deletions src/flow-control/log-timings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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`,
Expand Down

0 comments on commit 02b9082

Please sign in to comment.