Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance tests #335

Merged
merged 3 commits into from Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/concurrently.spec.ts
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
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
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
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