Skip to content

Commit b26ed0a

Browse files
committed
add typeDelay option
1 parent 7a838d6 commit b26ed0a

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const path = require('path');
2+
const { createCommandInterface } = require('../../lib');
3+
4+
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
5+
6+
test('should pass', async () => {
7+
const commandInterface = createCommandInterface('node ./multiple-prompts.js', {
8+
cwd: path.join(__dirname, '..'),
9+
logData: true,
10+
});
11+
await commandInterface.type('19\n');
12+
// await wait(100);
13+
await commandInterface.type('saurabh\n');
14+
const terminal = await commandInterface.getOutput();
15+
console.log(terminal);
16+
expect(1).toBe(1);
17+
});

lib/cli-testing-library.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ const { spawn } = require('child_process');
88
* tokenizedOutput: string,
99
* rawOutput: string
1010
* })} ParsedOutput
11+
*
12+
* @typedef {({
13+
* typeDelay: number,
14+
* logData: boolean,
15+
* cwd: string,
16+
* env: any
17+
* })} Options
1118
*/
1219

1320
// Big shoutout to https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797 for super cool reference to all ansi codes
@@ -38,6 +45,8 @@ const ANSI_CHARACTER_MAPS = {
3845
BOLD_2_END: `${ESC}23m`,
3946
}
4047

48+
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
49+
4150
const parseOutput = (output) => {
4251
const textAfter = (question) => {
4352
const questionIndex = output.indexOf(question) + question.length;
@@ -70,6 +79,11 @@ const parseOutput = (output) => {
7079
};
7180
};
7281

82+
/**
83+
*
84+
* @param {string} commandString
85+
* @param {Options} options
86+
*/
7387
const createCommandInterface = (commandString, options) => {
7488
// /** @type {import('child_process').ChildProcessWithoutNullStreams} */
7589
// let command;
@@ -102,11 +116,13 @@ const createCommandInterface = (commandString, options) => {
102116
throw error;
103117
});
104118

105-
const type = (text) => {
119+
const type = async (text) => {
106120
if (isFinishTypingCalled) {
107121
throw new Error('[cli-testing-library]: `type` cannot be called after `getOutput` or `finishTyping`');
108122
};
109123

124+
await wait(options.typeDelay ? options.typeDelay : 100);
125+
110126
return new Promise((resolve) => {
111127
command.stdin.write(`${text}`, () => {
112128
resolve();

0 commit comments

Comments
 (0)