|
| 1 | +import { Writable } from 'stream'; |
| 2 | + |
| 3 | +import { FakeCommand } from '../fixtures/fake-command'; |
| 4 | +import { OutputErrorHandler } from './output-error-handler'; |
| 5 | + |
| 6 | +let controller: OutputErrorHandler; |
| 7 | +let outputStream: Writable; |
| 8 | +let abortController: AbortController; |
| 9 | +let commands: FakeCommand[]; |
| 10 | +beforeEach(() => { |
| 11 | + commands = [new FakeCommand(), new FakeCommand()]; |
| 12 | + |
| 13 | + abortController = new AbortController(); |
| 14 | + outputStream = new Writable(); |
| 15 | + controller = new OutputErrorHandler({ abortController, outputStream }); |
| 16 | +}); |
| 17 | + |
| 18 | +it('returns same commands', () => { |
| 19 | + expect(controller.handle(commands)).toMatchObject({ commands }); |
| 20 | +}); |
| 21 | + |
| 22 | +describe('on output stream error', () => { |
| 23 | + beforeEach(() => { |
| 24 | + controller.handle(commands); |
| 25 | + outputStream.emit('error', new Error()); |
| 26 | + }); |
| 27 | + |
| 28 | + it('kills every command', () => { |
| 29 | + expect(commands[0].kill).toHaveBeenCalled(); |
| 30 | + expect(commands[1].kill).toHaveBeenCalled(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('sends abort signal', () => { |
| 34 | + expect(abortController.signal.aborted).toBe(true); |
| 35 | + }); |
| 36 | +}); |
| 37 | + |
| 38 | +describe('on finish', () => { |
| 39 | + it('unsubscribes from output stream error', () => { |
| 40 | + const { onFinish } = controller.handle(commands); |
| 41 | + onFinish(); |
| 42 | + |
| 43 | + outputStream.on('error', jest.fn()); |
| 44 | + outputStream.emit('error', new Error()); |
| 45 | + |
| 46 | + expect(commands[0].kill).not.toHaveBeenCalled(); |
| 47 | + expect(commands[1].kill).not.toHaveBeenCalled(); |
| 48 | + expect(abortController.signal.aborted).toBe(false); |
| 49 | + }); |
| 50 | +}); |
0 commit comments