Skip to content

Commit 02b9082

Browse files
authoredJul 4, 2022
Enhance tests (#335)
1 parent cc05702 commit 02b9082

File tree

4 files changed

+18
-20
lines changed

4 files changed

+18
-20
lines changed
 

‎src/concurrently.spec.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ import { concurrently, ConcurrentlyCommandInput, ConcurrentlyOptions } from './c
33
import { createFakeProcess, FakeCommand } from './fixtures/fake-command';
44
import { FlowController } from './flow-control/flow-controller';
55
import { Logger } from './logger';
6-
import { OutputWriter } from './output-writer';
7-
8-
jest.mock('./output-writer');
6+
import { Writable } from 'stream';
7+
import { createMockInstance } from 'jest-create-mock-instance';
98

109
let spawn: SpawnCommand;
1110
let kill: KillProcess;
@@ -48,10 +47,15 @@ it('spawns all commands', () => {
4847
expect(spawn).toHaveBeenCalledWith('kill', expect.objectContaining({}));
4948
});
5049

51-
it('output writer is created if logger is passed in options', () => {
50+
it('log output is passed to output stream if logger is specified in options', () => {
5251
const logger = new Logger({ hide: [] });
53-
create(['foo'], { logger });
54-
expect(OutputWriter).toHaveBeenCalledTimes(1);
52+
const outputStream = createMockInstance(Writable);
53+
create(['foo'], { logger, outputStream });
54+
logger.log('foo', 'bar');
55+
56+
expect(outputStream.write).toHaveBeenCalledTimes(2);
57+
expect(outputStream.write).toHaveBeenCalledWith('foo');
58+
expect(outputStream.write).toHaveBeenCalledWith('bar');
5559
});
5660

5761
it('spawns commands up to configured limit at once', () => {

‎src/concurrently.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export function concurrently(
176176
);
177177
commands = handleResult.commands;
178178

179-
if (options.logger) {
179+
if (options.logger && options.outputStream) {
180180
const outputWriter = new OutputWriter({
181181
outputStream: options.outputStream,
182182
group: options.group,

‎src/flow-control/log-timings.spec.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,14 @@ it('does not log timings summary if there was an error', () => {
109109
});
110110

111111
it('logs the sorted timings summary when all processes close successfully', () => {
112-
jest.spyOn(controller, 'printExitInfoTimingTable');
113112
controller.handle(commands);
114113

115114
commands[0].close.next(command0ExitInfo);
116115
commands[1].close.next(command1ExitInfo);
117116

117+
expect(logger.logGlobalEvent).toHaveBeenCalledTimes(1);
118+
expect(logger.logGlobalEvent).toHaveBeenCalledWith('Timings:');
118119
expect(logger.logTable).toHaveBeenCalledTimes(1);
119-
120-
// un-sorted ie by finish order
121-
expect(controller.printExitInfoTimingTable).toHaveBeenCalledWith([
122-
command0ExitInfo,
123-
command1ExitInfo,
124-
]);
125-
126120
// sorted by duration
127121
expect(logger.logTable).toHaveBeenCalledWith([
128122
LogTimings.mapCloseEventToTimingInfo(command1ExitInfo),

‎src/flow-control/log-timings.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ export class LogTimings implements FlowController {
5151
this.timestampFormat = timestampFormat;
5252
}
5353

54-
printExitInfoTimingTable(exitInfos: CloseEvent[]) {
54+
private printExitInfoTimingTable(exitInfos: CloseEvent[]) {
5555
const exitInfoTable = _(exitInfos)
5656
.sortBy(({ timings }) => timings.durationSeconds)
5757
.reverse()
5858
.map(LogTimings.mapCloseEventToTimingInfo)
5959
.value();
6060

61-
this.logger?.logGlobalEvent('Timings:');
62-
this.logger?.logTable(exitInfoTable);
61+
this.logger.logGlobalEvent('Timings:');
62+
this.logger.logTable(exitInfoTable);
6363
return exitInfos;
6464
}
6565

@@ -73,14 +73,14 @@ export class LogTimings implements FlowController {
7373
command.timer.subscribe(({ startDate, endDate }) => {
7474
if (!endDate) {
7575
const formattedStartDate = formatDate(startDate, this.timestampFormat);
76-
this.logger?.logCommandEvent(
76+
this.logger.logCommandEvent(
7777
`${command.command} started at ${formattedStartDate}`,
7878
command
7979
);
8080
} else {
8181
const durationMs = endDate.getTime() - startDate.getTime();
8282
const formattedEndDate = formatDate(endDate, this.timestampFormat);
83-
this.logger?.logCommandEvent(
83+
this.logger.logCommandEvent(
8484
`${
8585
command.command
8686
} stopped at ${formattedEndDate} after ${durationMs.toLocaleString()}ms`,

0 commit comments

Comments
 (0)
Please sign in to comment.