Skip to content

Commit 37f6046

Browse files
committed
test: mock process events
1 parent 10441df commit 37f6046

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

packages/prompts/src/index.test.ts

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { Readable, Writable } from 'node:stream';
1+
import { EventEmitter, Readable, Writable } from 'node:stream';
22
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test, vi } from 'vitest';
3-
import type { MockInstance } from 'vitest';
43
import * as prompts from './index.js';
54

65
// TODO (43081j): move this into a util?
@@ -66,20 +65,12 @@ describe.each(['true', 'false'])('prompts (isCI = %s)', (isCI) => {
6665
});
6766

6867
describe('spinner', () => {
69-
let processOnSpy: MockInstance;
70-
let processEmitSpy: MockInstance;
71-
7268
beforeEach(() => {
7369
vi.useFakeTimers();
74-
75-
// Spy on process methods
76-
processOnSpy = vi.spyOn(process, 'on');
77-
processEmitSpy = vi.spyOn(process, 'emit');
7870
});
7971

8072
afterEach(() => {
8173
vi.useRealTimers();
82-
vi.restoreAllMocks();
8374
});
8475

8576
test('returns spinner API', () => {
@@ -194,11 +185,31 @@ describe.each(['true', 'false'])('prompts (isCI = %s)', (isCI) => {
194185
});
195186

196187
describe('process exit handling', () => {
188+
let processEmitter: EventEmitter;
189+
190+
beforeEach(() => {
191+
processEmitter = new EventEmitter();
192+
193+
// Spy on process methods
194+
vi.spyOn(process, 'on').mockImplementation((ev, listener) => {
195+
processEmitter.on(ev, listener);
196+
return process;
197+
});
198+
vi.spyOn(process, 'removeListener').mockImplementation((ev, listener) => {
199+
processEmitter.removeListener(ev, listener);
200+
return process;
201+
});
202+
});
203+
204+
afterEach(() => {
205+
processEmitter.removeAllListeners();
206+
});
207+
197208
test('uses default cancel message', () => {
198209
const result = prompts.spinner({ output });
199210
result.start('Test operation');
200211

201-
process.emit('SIGINT');
212+
processEmitter.emit('SIGINT');
202213

203214
expect(output.buffer).toMatchSnapshot();
204215
});
@@ -210,7 +221,7 @@ describe.each(['true', 'false'])('prompts (isCI = %s)', (isCI) => {
210221
});
211222
result.start('Test operation');
212223

213-
process.emit('SIGINT');
224+
processEmitter.emit('SIGINT');
214225

215226
expect(output.buffer).toMatchSnapshot();
216227
});
@@ -222,7 +233,7 @@ describe.each(['true', 'false'])('prompts (isCI = %s)', (isCI) => {
222233
});
223234
result.start('Test operation');
224235

225-
process.emit('exit', 2);
236+
processEmitter.emit('exit', 2);
226237

227238
expect(output.buffer).toMatchSnapshot();
228239
});
@@ -237,7 +248,7 @@ describe.each(['true', 'false'])('prompts (isCI = %s)', (isCI) => {
237248
const result = prompts.spinner({ output });
238249
result.start('Test operation');
239250

240-
process.emit('SIGINT');
251+
processEmitter.emit('SIGINT');
241252

242253
expect(output.buffer).toMatchSnapshot();
243254

@@ -259,7 +270,7 @@ describe.each(['true', 'false'])('prompts (isCI = %s)', (isCI) => {
259270
const result = prompts.spinner({ output });
260271
result.start('Test operation');
261272

262-
process.emit('exit', 2);
273+
processEmitter.emit('exit', 2);
263274

264275
expect(output.buffer).toMatchSnapshot();
265276

@@ -286,7 +297,7 @@ describe.each(['true', 'false'])('prompts (isCI = %s)', (isCI) => {
286297
});
287298
result.start('Test operation');
288299

289-
process.emit('SIGINT');
300+
processEmitter.emit('SIGINT');
290301
expect(output.buffer).toMatchSnapshot();
291302

292303
// Reset buffer
@@ -299,7 +310,7 @@ describe.each(['true', 'false'])('prompts (isCI = %s)', (isCI) => {
299310
});
300311
result2.start('Test operation');
301312

302-
process.emit('exit', 2);
313+
processEmitter.emit('exit', 2);
303314
expect(output.buffer).toMatchSnapshot();
304315

305316
// Reset to defaults

0 commit comments

Comments
 (0)