-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathinquirer.test.mts
1018 lines (890 loc) · 25.8 KB
/
inquirer.test.mts
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Inquirer public API test
*/
import fs from 'node:fs';
import os from 'node:os';
import stream from 'node:stream';
import tty from 'node:tty';
import readline from 'node:readline';
import { vi, expect, beforeEach, afterEach, describe, it, expectTypeOf } from 'vitest';
import { of } from 'rxjs';
import { AbortPromptError, createPrompt } from '@inquirer/core';
import type { InquirerReadline } from '@inquirer/type';
import inquirer, { type QuestionMap } from './src/index.mjs';
import type { Answers } from './src/types.mjs';
import { _ } from './src/ui/prompt.mjs';
declare module './src/index.mjs' {
interface QuestionMap {
stub: { answer?: string | boolean; message: string; default?: string };
stub2: { answer?: string | boolean; message: string; default: string };
stubSelect: { choices: string[] };
failing: { message: string };
}
}
type TestQuestions = {
stub: { answer?: string | boolean; message: string };
stub2: { answer?: string | boolean; message: string; default: string };
stubSelect: { choices: string[] };
failing: { message: string };
};
function throwFunc(step: any): any {
throw new Error(`askAnswered Error ${step}`);
}
class StubPrompt {
question: QuestionMap['stub'];
constructor(question: QuestionMap['stub']) {
this.question = question;
}
run() {
return Promise.resolve(this.question.answer ?? 'bar');
}
close() {}
}
class StubFailingPrompt {
run() {
return Promise.reject(new Error('This test prompt always reject'));
}
close() {}
}
class StubEventualyFailingPrompt {
timeout?: NodeJS.Timeout;
run() {
this.timeout = setTimeout(() => {}, 1000);
return Promise.reject(new Error('This test prompt always reject'));
}
close() {
clearTimeout(this.timeout);
}
}
beforeEach(() => {
inquirer.restoreDefaultPrompts();
inquirer.registerPrompt('stub', StubPrompt);
inquirer.registerPrompt('failing', StubFailingPrompt);
});
describe('inquirer.prompt(...)', () => {
describe('interfaces', () => {
it('takes a prompts array', async () => {
const answers = await inquirer.prompt([
{
type: 'stub',
name: 'q1',
message: 'message',
},
{
type: 'stub',
name: 'q2',
message: 'message',
},
]);
expect(answers).toEqual({
q1: 'bar',
q2: 'bar',
});
expectTypeOf(answers).toEqualTypeOf<{ q1: any; q2: any }>();
});
it('takes a question map object', async () => {
const answers = await inquirer.prompt({
q1: {
type: 'stub',
message: 'message',
},
q2: {
type: 'stub',
answer: 'Foo',
message: 'message',
},
});
expect(answers).toEqual({
q1: 'bar',
q2: 'Foo',
});
expectTypeOf(answers).toEqualTypeOf<{ q1: any; q2: any }>();
});
it('takes a single prompt', async () => {
const answers = await inquirer.prompt({
type: 'stub',
name: 'q1',
message: 'message',
});
expect(answers).toEqual({ q1: 'bar' });
expectTypeOf(answers).toEqualTypeOf<{ q1: any }>();
});
it('takes an Observable', async () => {
const answers = await inquirer.prompt(
of(
{
type: 'stub',
name: 'q1',
message: 'message',
answer: true,
} as const,
{
type: 'stub',
name: 'q2',
message: 'message',
answer: false,
} as const,
),
);
expect(answers).toEqual({ q1: true, q2: false });
expectTypeOf(answers).toEqualTypeOf<{ q1: any; q2: any }>();
});
});
it("should close and create a new readline instances each time it's called", async () => {
const actualCreateInterface = readline.createInterface;
vi.spyOn(readline, 'createInterface').mockImplementation((opts) => {
const rl = actualCreateInterface(opts) as InquirerReadline;
vi.spyOn(rl, 'close');
vi.spyOn(rl.output, 'end');
return rl;
});
const promise = inquirer.prompt([
{
type: 'stub',
name: 'q1',
message: 'message',
},
]);
await promise;
const rl1 = vi.mocked(readline.createInterface).mock.results[0]!
.value as InquirerReadline;
expect(rl1.close).toHaveBeenCalledTimes(1);
expect(rl1.output.end).toHaveBeenCalledTimes(1);
const promise2 = inquirer.prompt([
{
type: 'stub',
name: 'q1',
message: 'message',
},
]);
await promise2;
const rl2 = vi.mocked(readline.createInterface).mock.results[1]!
.value as InquirerReadline;
expect(rl2.close).toHaveBeenCalledTimes(1);
expect(rl2.output.end).toHaveBeenCalledTimes(1);
expect(rl1).not.toBe(rl2);
});
it('should close readline instance on rejected promise', async () => {
const actualCreateInterface = readline.createInterface;
vi.spyOn(readline, 'createInterface').mockImplementation((opts) => {
const rl = actualCreateInterface(opts) as InquirerReadline;
vi.spyOn(rl, 'close');
vi.spyOn(rl.output, 'end');
return rl;
});
const promise = inquirer.prompt([
{
type: 'failing',
name: 'q1',
message: 'message',
},
]);
await promise.catch(() => {
const rl = vi.mocked(readline.createInterface).mock.results[0]!
.value as InquirerReadline;
expect(rl.close).toHaveBeenCalledTimes(1);
expect(rl.output.end).toHaveBeenCalledTimes(1);
});
});
it('should take a prompts array with nested names', async () => {
const answers = await inquirer.prompt([
{
type: 'stub',
name: 'foo.bar.q1',
message: 'message',
},
{
type: 'stub',
name: 'foo.q2',
message: 'message',
},
]);
expect(answers).toEqual({
foo: {
bar: {
q1: 'bar',
},
q2: 'bar',
},
});
});
it('should parse `message` if passed as a function', async () => {
const stubMessage = 'foo';
class FakePrompt {
constructor(question: QuestionMap['stub']) {
expect(question.message).toEqual(stubMessage);
}
run() {
return Promise.resolve();
}
close() {}
}
inquirer.registerPrompt('stub', FakePrompt);
await inquirer.prompt(
[
{
type: 'stub',
name: 'name',
message(answers) {
expectTypeOf(answers).toEqualTypeOf<Partial<{ name: any; name1: string }>>();
expect(answers).toEqual({ name1: 'bar' });
return stubMessage;
},
},
],
{ name1: 'bar' },
);
});
it('should run asynchronous `message`', async () => {
const stubMessage = 'Stub message';
class FakePrompt {
question: QuestionMap['stub2'];
constructor(question: QuestionMap['stub2']) {
this.question = question;
expect(question.message).toEqual(stubMessage);
}
run() {
return Promise.resolve(this.question.answer);
}
close() {}
}
inquirer.registerPrompt('stub2', FakePrompt);
const answers = await inquirer.prompt([
{
type: 'stub',
name: 'name1',
answer: 'bar',
message: 'message',
},
{
type: 'stub',
name: 'name2',
answer: 'foo',
message(answers) {
expectTypeOf(answers).toEqualTypeOf<Partial<{ name1: any; name2: any }>>();
expect(answers).toEqual({ name1: 'bar' });
const goOn = this.async();
setTimeout(() => {
goOn(null, stubMessage);
}, 0);
},
},
]);
expect(answers).toEqual({ name1: 'bar', name2: 'foo' });
});
it('should parse `default` if passed as a function', async () => {
await inquirer.prompt([
{
type: 'stub',
name: 'name1',
message: 'message',
answer: 'bar',
},
{
type: 'stub',
name: 'name',
message: 'message',
default(answers) {
expect(answers.name1).toEqual('bar');
return 'foo';
},
},
]);
});
it('should run asynchronous `default`', async () => {
let goesInDefault = false;
const input2Default = 'foo';
class Stub2Prompt {
constructor(question: QuestionMap['stub2']) {
expect(question.default).toEqual(input2Default);
}
run() {
return Promise.resolve();
}
close() {}
}
inquirer.registerPrompt('stub2', Stub2Prompt);
await inquirer.prompt([
{
type: 'stub',
name: 'name1',
message: 'message',
answer: 'bar',
},
{
type: 'stub2',
name: 'q2',
message: 'message',
default(answers) {
goesInDefault = true;
expectTypeOf(answers).toEqualTypeOf<Partial<{ name1: any; q2: any }>>();
expect(answers).toEqual({ name1: 'bar' });
const goOn = this.async();
setTimeout(() => {
goOn(null, input2Default);
}, 0);
},
},
]);
expect(goesInDefault).toEqual(true);
});
it('should pass previous answers to the prompt constructor', async () => {
class Stub2Prompt {
constructor(
_question: Record<string, any>,
_rl: InquirerReadline,
answers: Answers,
) {
expect(answers['name1']).toEqual('bar');
}
run() {
return Promise.resolve();
}
close() {}
}
inquirer.registerPrompt('stub2', Stub2Prompt);
await inquirer.prompt([
{
type: 'stub',
name: 'name1',
answer: 'bar',
message: 'message',
},
{
type: 'stub2',
name: 'name',
message: 'message',
},
]);
});
it('should parse `choices` if passed as a function', async () => {
const stubChoices = ['foo', 'bar'];
class FakeSelect {
constructor(question: QuestionMap['stubSelect']) {
expect(question.choices).toEqual(
stubChoices.map((choice) => ({ name: choice, value: choice })),
);
}
run() {
return Promise.resolve();
}
close() {}
}
inquirer.registerPrompt('stubSelect', FakeSelect);
const answers = await inquirer.prompt([
{
type: 'stub',
name: 'name1',
message: 'message',
answer: 'bar',
},
{
type: 'stubSelect',
name: 'name',
message: 'message',
choices(answers) {
expectTypeOf(answers).toEqualTypeOf<Partial<{ name1: any; name: any }>>();
expect(answers).toEqual({ name1: 'bar' });
return stubChoices;
},
},
]);
expect(answers).toEqual({ name1: 'bar', name: undefined });
});
it('should expose the Reactive interface', async () => {
const spy = vi.fn();
const promise = inquirer.prompt([
{
type: 'stub',
name: 'name1',
message: 'message',
answer: 'bar',
},
{
type: 'stub',
name: 'name',
message: 'message',
answer: 'doe',
},
]);
promise.ui.process.subscribe(spy);
await promise;
expect(spy).toHaveBeenCalledWith({ name: 'name1', answer: 'bar' });
expect(spy).toHaveBeenCalledWith({ name: 'name', answer: 'doe' });
});
it('should expose the UI', async () => {
const promise = inquirer.prompt([]);
expect(promise.ui.answers).toBeTypeOf('object');
await promise;
});
describe('hierarchical mode (`when`)', () => {
it('should pass current answers to `when`', async () => {
const answers = await inquirer.prompt([
{
type: 'stub',
name: 'q1',
message: 'message',
},
{
type: 'stub',
name: 'q2',
message: 'message',
when(answers) {
expect(answers).toEqual({ q1: 'bar' });
return true;
},
},
]);
expect(answers).toEqual({
q1: 'bar',
q2: 'bar',
});
});
it('should run prompt if `when` returns true', async () => {
const when = vi.fn(() => true);
const answers = await inquirer.prompt([
{
type: 'stub',
name: 'q1',
message: 'message',
},
{
type: 'stub',
name: 'q2',
message: 'message',
when,
},
]);
expect(when).toHaveBeenCalledOnce();
expect(answers).toEqual({ q1: 'bar', q2: 'bar' });
});
it('should run prompt if `when` is true', async () => {
const answers = await inquirer.prompt([
{
type: 'stub',
name: 'q1',
message: 'message',
},
{
type: 'stub',
name: 'q2',
message: 'message',
when: true,
},
]);
expect(answers).toEqual({ q1: 'bar', q2: 'bar' });
});
it('should not run prompt if `when` returns false', async () => {
const when = vi.fn(() => false);
const answers = await inquirer.prompt([
{
type: 'stub',
name: 'q1',
message: 'message',
},
{
type: 'stub',
name: 'q2',
message: 'message',
when,
},
{
type: 'stub',
name: 'q3',
message: 'message',
},
]);
expect(when).toHaveBeenCalledOnce();
expect(answers).toEqual({ q1: 'bar', q3: 'bar' });
});
it('should not run prompt if `when` is false', async () => {
const answers = await inquirer.prompt([
{
type: 'stub',
name: 'q1',
message: 'message',
},
{
type: 'stub',
name: 'q2',
message: 'message',
when: false,
},
{
type: 'stub',
name: 'q3',
message: 'message',
default: 'foo',
},
]);
expect(answers).toEqual({ q1: 'bar', q3: 'bar' });
});
it('should run asynchronous `when`', async () => {
let goesInWhen = false;
const answers = await inquirer.prompt([
{
type: 'stub',
name: 'q1',
message: 'message',
},
{
type: 'stub',
name: 'q2',
message: 'message',
answer: 'answer from running',
when(answers) {
expect(answers).toEqual({ q1: 'bar' });
expectTypeOf(answers).toEqualTypeOf<Partial<{ q1: any; q2: any }>>();
goesInWhen = true;
const goOn = this.async();
setTimeout(() => {
goOn(null, true);
}, 0);
},
},
]);
expect(goesInWhen).toEqual(true);
expect(answers).toEqual({ q1: 'bar', q2: 'answer from running' });
});
it('should get the value which set in `when` on returns false', async () => {
const answers = await inquirer.prompt([
{
type: 'input',
name: 'q',
message: 'message',
when(answers) {
expectTypeOf(answers).toEqualTypeOf<Partial<{ q: any }>>();
answers.q = 'foo';
return false;
},
},
]);
expect(answers).toEqual({ q: 'foo' });
});
});
describe('Prefilling answers', () => {
it('should take a prompts array and answers and return answers', async () => {
const answers = await inquirer.prompt(
[
{
type: 'stub',
name: 'q1',
message: 'message',
answer: 'stub answer',
},
],
{ prefilled: true },
);
expect(answers).toEqual({
q1: 'stub answer',
prefilled: true,
});
expectTypeOf(answers).toEqualTypeOf<{ q1: any; prefilled: boolean }>();
});
it('should not run prompt if answer exists for question', async () => {
const answers = await inquirer.prompt(
[
{
type: 'input',
name: 'prefilled',
when: throwFunc,
validate: throwFunc,
transformer: throwFunc,
message: 'message',
default: 'newValue',
},
],
{ prefilled: 'prefilled' },
);
expect(answers).toEqual({ prefilled: 'prefilled' });
});
it('should not run prompt if nested answer exists for question', async () => {
const answers = await inquirer.prompt(
[
{
type: 'input',
name: 'prefilled.nested',
when: throwFunc,
validate: throwFunc,
transformer: throwFunc,
message: 'message',
default: 'newValue',
},
],
{
prefilled: { nested: 'prefilled' },
},
);
expect(answers.prefilled.nested).toEqual('prefilled');
// @ts-expect-error TODO fix types around nested types.
expectTypeOf(answers).toEqualTypeOf<{ prefilled: { nested: string } }>();
});
it('should run prompt if answer exists for question and askAnswered is set', async () => {
const answers = await inquirer.prompt(
[
{
askAnswered: true,
type: 'stub',
name: 'prefilled',
message: 'message',
answer: 'bar',
},
],
{ prefilled: 'prefilled' },
);
expect(answers).toEqual({ prefilled: 'bar' });
});
it('should run prompt if nested answer exists for question and askAnswered is set', async () => {
const answers = await inquirer.prompt(
[
{
askAnswered: true,
type: 'stub',
name: 'prefilled.nested',
message: 'message',
answer: 'newValue',
},
],
{
prefilled: { nested: 'prefilled' },
},
);
expect(answers).toEqual({ prefilled: { nested: 'newValue' } });
// @ts-expect-error TODO fix types around nested types.
expectTypeOf(answers).toEqualTypeOf<{ prefilled: { nested: string } }>();
});
});
describe('#registerPrompt()', () => {
it('register new prompt types', async () => {
class FakePrompt {
constructor(
question: { type: 'stub'; name: string; message: string },
_rl: InquirerReadline,
answers: Answers,
) {
expect(question).toEqual({ type: 'stub2', name: 'foo', message: 'message' });
expect(answers).toEqual({ extra: 'bar' });
}
run() {
return Promise.resolve('bar');
}
close() {}
}
inquirer.registerPrompt('stub2', FakePrompt);
const answers = await inquirer.prompt([
{ type: 'stub', name: 'extra', message: 'message' },
{ type: 'stub2', name: 'foo', message: 'message' },
]);
expect(answers).toEqual({ extra: 'bar', foo: 'bar' });
});
});
describe('#restoreDefaultPrompts()', () => {
it('restore default prompts', () => {
class StubPrompt {
run = vi.fn(() => {
return Promise.resolve('bar');
});
close() {}
}
const ConfirmPrompt = inquirer.prompt.prompts['confirm'];
inquirer.registerPrompt('confirm', StubPrompt);
inquirer.restoreDefaultPrompts();
expect(ConfirmPrompt).toEqual(inquirer.prompt.prompts['confirm']);
});
});
});
describe('AbortSignal support', () => {
it('throws on aborted signal', async () => {
const localPrompt = inquirer.createPromptModule<TestQuestions>({
signal: AbortSignal.abort(),
});
localPrompt.registerPrompt('stub', StubEventualyFailingPrompt);
const promise = localPrompt({ type: 'stub', name: 'q1', message: 'message' });
await expect(promise).rejects.toThrow(AbortPromptError);
});
it('legacy prompts can be aborted by module signal', async () => {
const abortController = new AbortController();
const localPrompt = inquirer.createPromptModule<TestQuestions>({
signal: abortController.signal,
});
localPrompt.registerPrompt('stub', StubEventualyFailingPrompt);
const promise = localPrompt({ type: 'stub', name: 'q1', message: 'message' });
abortController.abort();
await expect(promise).rejects.toThrow(AbortPromptError);
});
it('modern prompts can be aborted by module signal', async () => {
const abortController = new AbortController();
const localPrompt = inquirer.createPromptModule<TestQuestions>({
signal: abortController.signal,
});
localPrompt.registerPrompt(
'stub',
createPrompt(() => 'dummy prompt'),
);
const promise = localPrompt({ type: 'stub', name: 'q1', message: 'message' });
abortController.abort();
await expect(promise).rejects.toThrow(AbortPromptError);
});
it('modern prompts can be aborted using ui.close()', async () => {
const localPrompt = inquirer.createPromptModule<TestQuestions>();
localPrompt.registerPrompt(
'stub',
createPrompt(() => 'dummy prompt'),
);
const promise = localPrompt({ type: 'stub', name: 'q1', message: 'message' });
promise.ui.close();
await expect(promise).rejects.toThrow(AbortPromptError);
});
});
describe('Non-TTY checks', () => {
let original: boolean;
beforeEach(() => {
original = process.stdin.isTTY;
process.stdin.isTTY = false;
});
afterEach(() => {
process.stdin.isTTY = original;
});
it('Throw an exception when run in non-tty', async () => {
const localPrompt = inquirer.createPromptModule<TestQuestions>({
skipTTYChecks: false,
});
localPrompt.registerPrompt('stub', StubPrompt);
const promise = localPrompt([
{
type: 'stub',
name: 'q1',
message: 'message',
},
]);
await expect(promise).rejects.toHaveProperty('isTtyError', true);
});
it("Don't throw an exception when run in non-tty by default ", async () => {
const localPrompt = inquirer.createPromptModule<TestQuestions>();
localPrompt.registerPrompt('stub', StubPrompt);
await localPrompt([
{
type: 'stub',
name: 'q1',
message: 'message',
},
{
type: 'stub',
name: 'q2',
message: 'message',
},
]);
});
it("Don't throw an exception when run in non-tty and skipTTYChecks is true ", async () => {
const localPrompt = inquirer.createPromptModule<TestQuestions>({
skipTTYChecks: true,
});
localPrompt.registerPrompt('stub', StubPrompt);
await localPrompt([
{
type: 'stub',
name: 'q1',
message: 'message',
},
{
type: 'stub',
name: 'q2',
message: 'message',
},
]);
});
it("Don't throw an exception when run in non-tty and custom input is provided async ", async () => {
const localPrompt = inquirer.createPromptModule<TestQuestions>({
input: new stream.Readable({
// We must have a default read implementation
// for this to work, if not it will error out
// with the following error message during testing
// Uncaught Error [ERR_METHOD_NOT_IMPLEMENTED]: The _read() method is not implemented
read() {},
}),
});
localPrompt.registerPrompt('stub', StubPrompt);
await localPrompt([
{
type: 'stub',
name: 'q1',
message: 'message',
},
{
type: 'stub',
name: 'q2',
message: 'message',
},
]);
});
it('Throw an exception when run in non-tty and custom input is provided with skipTTYChecks: false', async () => {
const localPrompt = inquirer.createPromptModule<TestQuestions>({
input: new stream.Readable(),
skipTTYChecks: false,
});
localPrompt.registerPrompt('stub', StubPrompt);
const promise = localPrompt([
{
type: 'stub',
name: 'q1',
message: 'message',
},
]);
await expect(promise).rejects.toHaveProperty('isTtyError', true);
});
const itSkipWindows =
os.type() === 'Windows_NT' || process.env['GITHUB_ACTIONS'] ? it.skip : it;
itSkipWindows('No exception when using tty other than process.stdin', async () => {
const input = new tty.ReadStream(fs.openSync('/dev/tty', 'r+'));
// Uses manually opened tty as input instead of process.stdin
const localPrompt = inquirer.createPromptModule<TestQuestions>({
input,
skipTTYChecks: false,
});
localPrompt.registerPrompt('stub', StubPrompt);
const promise = localPrompt([
{
type: 'stub',
name: 'q1',
message: 'message',
},
]);
// Release the input tty socket
input.unref();
await expect(promise).resolves.toEqual({ q1: 'bar' });
});
});
describe('set utility function tests', () => {
/* eslint-disable @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access */
it('Should set an objects property when provided a path and a value', () => {
const obj: any = {};
const path = 'a.b';
const value = 'c';
_.set(obj, path, value);
expect(obj.a.b).toBe('c');
});
it('Should set an objects property when provided a path and an array value', () => {
const obj: any = {};
const path = 'a.b';
const value = ['c', 'd'];
_.set(obj, path, value);
expect(obj.a.b[0]).toBe('c');
expect(obj.a.b[1]).toBe('d');
});
it('Should replace a boolean with an object when a path is provided that overrides that boolean', () => {
const obj: any = { a: true };
const path = 'a.b';
const value = 'c';