Skip to content

Commit

Permalink
Fix(@inquirer/core): End output stream on exit (instead of destroy) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
SBoudrias committed Aug 5, 2024
1 parent 04323b3 commit 87b58aa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
35 changes: 35 additions & 0 deletions packages/core/core.test.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AsyncResource } from 'node:async_hooks';
import { Stream } from 'node:stream';
import { describe, it, expect, vi } from 'vitest';
import { render } from '@inquirer/testing';
import stripAnsi from 'strip-ansi';
Expand Down Expand Up @@ -509,6 +510,40 @@ describe('createPrompt()', () => {
await expect(answer).resolves.toBe('closed');
expect(exitSpy).toHaveBeenCalledTimes(1);
});

it('release listeners when done', async () => {
class WritableStream extends Stream.Writable {
override _write() {}
}

const Prompt = (config: { message: string }, done: (value: string) => void) => {
useKeypress((key: KeypressEvent) => {
if (isEnterKey(key)) {
done('done');
}
});

return config.message;
};
const prompt = createPrompt(Prompt);

const warningSpy = vi.fn();
process.on('warning', warningSpy);

// We need to reuse the same stream to ensure it gets cleaned up properly.
const output = new WritableStream();
for (let i = 0; i < 15; i++) {
const { answer, events } = await render(
prompt,
{ message: `Question ${i}` },
{ output },
);
events.keypress('enter');
await expect(answer).resolves.toEqual('done');
}

expect(warningSpy).not.toHaveBeenCalled();
});
});

it('allow cancelling the prompt multiple times', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/lib/create-prompt.mts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function createPrompt<Value, Config>(view: ViewFunction<Value, Config>) {
removeExitListener();
rl.input.removeListener('keypress', checkCursorPos);
rl.removeListener('close', hooksCleanup);
output.destroy();
output.end();
}

cancel = () => {
Expand Down

0 comments on commit 87b58aa

Please sign in to comment.