Skip to content

Commit 7073e1d

Browse files
committed
feat: add run-command error logs showing
1 parent ee69b67 commit 7073e1d

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

__tests__/utils/run-command.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ beforeEach(() => {
1212

1313
class ChildMock extends EventEmitter {
1414
public stdout = new EventEmitter();
15+
public stderr = new EventEmitter();
1516
}
1617

1718
const spawnMock = <EV>(event: 'exit' | 'error' = 'exit', emitValue?: EV, delayMs = 200) => {
@@ -72,6 +73,25 @@ describe('spawnCommand', () => {
7273

7374
expect(result).toBe(chunks.join(''));
7475
});
76+
77+
test('should write error messages', async () => {
78+
const child = spawnMock('exit');
79+
const chunks = ['Hello', 'World'].map((value) => Buffer.from(value, 'utf-8'));
80+
81+
const spy = jest.spyOn(global.console, 'error').mockImplementation(jest.fn());
82+
const spawnResult = spawnCommand(command, args);
83+
84+
for (const chunk of chunks) {
85+
child.stderr.emit('data', chunk);
86+
}
87+
88+
await spawnResult;
89+
90+
expect(spy).toHaveBeenCalledWith('Hello');
91+
expect(spy).toHaveBeenCalledWith('World');
92+
93+
spy.mockRestore();
94+
});
7595
});
7696

7797
describe('runCommand', () => {

src/utils/run-command.ts

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ export const spawnCommand = async (command: string, args: string[]): Promise<str
66
const child = spawn(command, args);
77
let result = Buffer.alloc(0);
88

9+
child?.stderr?.on('data', (chunk) => {
10+
console.error(chunk.toString());
11+
});
912
child.on('error', (err) => rej(err));
1013
child.on('exit', () => res(result.toString('utf-8')));
1114

0 commit comments

Comments
 (0)