/
concurrently.spec.ts
274 lines (237 loc) · 8.94 KB
/
concurrently.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import { ChildProcess, KillProcess, SpawnCommand } from './command';
import { concurrently, ConcurrentlyCommandInput, ConcurrentlyOptions } from './concurrently';
import { createFakeProcess, FakeCommand } from './fixtures/fake-command';
import { FlowController } from './flow-control/flow-controller';
import { Logger } from './logger';
import { Writable } from 'stream';
import { createMockInstance } from 'jest-create-mock-instance';
let spawn: SpawnCommand;
let kill: KillProcess;
let onFinishHooks: (() => void)[];
let controllers: jest.Mocked<FlowController>[];
let processes: ChildProcess[];
const create = (commands: ConcurrentlyCommandInput[], options: Partial<ConcurrentlyOptions> = {}) =>
concurrently(commands, Object.assign(options, { controllers, spawn, kill }));
beforeEach(() => {
processes = [];
spawn = jest.fn(() => {
const process = createFakeProcess(processes.length);
processes.push(process);
return process;
});
kill = jest.fn();
onFinishHooks = [jest.fn(), jest.fn()];
controllers = [
{ handle: jest.fn(commands => ({ commands, onFinish: onFinishHooks[0] })) },
{ handle: jest.fn(commands => ({ commands, onFinish: onFinishHooks[1] })) },
];
});
it('fails if commands is not an array', () => {
const bomb = () => create('foo' as never);
expect(bomb).toThrowError();
});
it('fails if no commands were provided', () => {
const bomb = () => create([]);
expect(bomb).toThrowError();
});
it('spawns all commands', () => {
create(['echo', 'kill']);
expect(spawn).toHaveBeenCalledTimes(2);
expect(spawn).toHaveBeenCalledWith('echo', expect.objectContaining({}));
expect(spawn).toHaveBeenCalledWith('kill', expect.objectContaining({}));
});
it('log output is passed to output stream if logger is specified in options', () => {
const logger = new Logger({ hide: [] });
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', () => {
create(['foo', 'bar', 'baz', 'qux'], { maxProcesses: 2 });
expect(spawn).toHaveBeenCalledTimes(2);
expect(spawn).toHaveBeenCalledWith('foo', expect.objectContaining({}));
expect(spawn).toHaveBeenCalledWith('bar', expect.objectContaining({}));
// Test out of order completion picking up new processes in-order
processes[1].emit('close', 1, null);
expect(spawn).toHaveBeenCalledTimes(3);
expect(spawn).toHaveBeenCalledWith('baz', expect.objectContaining({}));
processes[0].emit('close', null, 'SIGINT');
expect(spawn).toHaveBeenCalledTimes(4);
expect(spawn).toHaveBeenCalledWith('qux', expect.objectContaining({}));
// Shouldn't attempt to spawn anything else.
processes[2].emit('close', 1, null);
expect(spawn).toHaveBeenCalledTimes(4);
});
it('runs controllers with the commands', () => {
create(['echo', '"echo wrapped"']);
controllers.forEach(controller => {
expect(controller.handle).toHaveBeenCalledWith([
expect.objectContaining({ command: 'echo', index: 0 }),
expect.objectContaining({ command: 'echo wrapped', index: 1 }),
]);
});
});
it('runs commands with a name or prefix color', () => {
create([{ command: 'echo', prefixColor: 'red', name: 'foo' }, 'kill']);
controllers.forEach(controller => {
expect(controller.handle).toHaveBeenCalledWith([
expect.objectContaining({ command: 'echo', index: 0, name: 'foo', prefixColor: 'red' }),
expect.objectContaining({ command: 'kill', index: 1, name: '', prefixColor: '' }),
]);
});
});
it('runs commands with a list of colors', () => {
create(['echo', 'kill'], {
prefixColors: ['red'],
});
controllers.forEach(controller => {
expect(controller.handle).toHaveBeenCalledWith([
expect.objectContaining({ command: 'echo', prefixColor: 'red' }),
expect.objectContaining({ command: 'kill', prefixColor: 'red' }),
]);
});
});
it('passes commands wrapped from a controller to the next one', () => {
const fakeCommand = new FakeCommand('banana', 'banana');
controllers[0].handle.mockReturnValue({ commands: [fakeCommand] });
create(['echo']);
expect(controllers[0].handle).toHaveBeenCalledWith([
expect.objectContaining({ command: 'echo', index: 0 }),
]);
expect(controllers[1].handle).toHaveBeenCalledWith([fakeCommand]);
expect(fakeCommand.start).toHaveBeenCalledTimes(1);
});
it('merges extra env vars into each command', () => {
create([
{ command: 'echo', env: { foo: 'bar' } },
{ command: 'echo', env: { foo: 'baz' } },
'kill',
]);
expect(spawn).toHaveBeenCalledTimes(3);
expect(spawn).toHaveBeenCalledWith(
'echo',
expect.objectContaining({
env: expect.objectContaining({ foo: 'bar' }),
})
);
expect(spawn).toHaveBeenCalledWith(
'echo',
expect.objectContaining({
env: expect.objectContaining({ foo: 'baz' }),
})
);
expect(spawn).toHaveBeenCalledWith(
'kill',
expect.objectContaining({
env: expect.not.objectContaining({ foo: expect.anything() }),
})
);
});
it('uses cwd from options for each command', () => {
create(
[
{ command: 'echo', env: { foo: 'bar' } },
{ command: 'echo', env: { foo: 'baz' } },
'kill',
],
{
cwd: 'foobar',
}
);
expect(spawn).toHaveBeenCalledTimes(3);
expect(spawn).toHaveBeenCalledWith(
'echo',
expect.objectContaining({
env: expect.objectContaining({ foo: 'bar' }),
cwd: 'foobar',
})
);
expect(spawn).toHaveBeenCalledWith(
'echo',
expect.objectContaining({
env: expect.objectContaining({ foo: 'baz' }),
cwd: 'foobar',
})
);
expect(spawn).toHaveBeenCalledWith(
'kill',
expect.objectContaining({
env: expect.not.objectContaining({ foo: expect.anything() }),
cwd: 'foobar',
})
);
});
it('uses overridden cwd option for each command if specified', () => {
create(
[
{ command: 'echo', env: { foo: 'bar' }, cwd: 'baz' },
{ command: 'echo', env: { foo: 'baz' } },
],
{
cwd: 'foobar',
}
);
expect(spawn).toHaveBeenCalledTimes(2);
expect(spawn).toHaveBeenCalledWith(
'echo',
expect.objectContaining({
env: expect.objectContaining({ foo: 'bar' }),
cwd: 'baz',
})
);
expect(spawn).toHaveBeenCalledWith(
'echo',
expect.objectContaining({
env: expect.objectContaining({ foo: 'baz' }),
cwd: 'foobar',
})
);
});
it('argument placeholders are properly replaced when additional arguments are passed', () => {
create(
[
{ command: 'echo {1}' },
{ command: 'echo {@}' },
{ command: 'echo {*}' },
{ command: 'echo \\{@}' },
],
{
additionalArguments: ['foo', 'bar'],
}
);
expect(spawn).toHaveBeenCalledTimes(4);
expect(spawn).toHaveBeenCalledWith('echo foo', expect.objectContaining({}));
expect(spawn).toHaveBeenCalledWith('echo foo bar', expect.objectContaining({}));
expect(spawn).toHaveBeenCalledWith("echo 'foo bar'", expect.objectContaining({}));
expect(spawn).toHaveBeenCalledWith('echo {@}', expect.objectContaining({}));
});
it('argument placeholders are not replaced when additional arguments are not defined', () => {
create([
{ command: 'echo {1}' },
{ command: 'echo {@}' },
{ command: 'echo {*}' },
{ command: 'echo \\{@}' },
]);
expect(spawn).toHaveBeenCalledTimes(4);
expect(spawn).toHaveBeenCalledWith('echo {1}', expect.objectContaining({}));
expect(spawn).toHaveBeenCalledWith('echo {@}', expect.objectContaining({}));
expect(spawn).toHaveBeenCalledWith('echo {*}', expect.objectContaining({}));
expect(spawn).toHaveBeenCalledWith('echo {@}', expect.objectContaining({}));
});
it('runs onFinish hook after all commands run', async () => {
const promise = create(['foo', 'bar'], { maxProcesses: 1 });
expect(spawn).toHaveBeenCalledTimes(1);
expect(onFinishHooks[0]).not.toHaveBeenCalled();
expect(onFinishHooks[1]).not.toHaveBeenCalled();
processes[0].emit('close', 0, null);
expect(spawn).toHaveBeenCalledTimes(2);
expect(onFinishHooks[0]).not.toHaveBeenCalled();
expect(onFinishHooks[1]).not.toHaveBeenCalled();
processes[1].emit('close', 0, null);
await promise.result;
expect(onFinishHooks[0]).toHaveBeenCalled();
expect(onFinishHooks[1]).toHaveBeenCalled();
});