You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sometimes, we want to simulate user input , for example we need test when we write a npm module.
like following code ,it will block at the second prompt , can't see the second question.
constinquirer=require('inquirer');constchoices=[{name: "test1",value: "test1"},{name: "test2",value: "test2"}];constchoices2=[{name: "test1",value: "test1"},{name: "test2",value: "test2"}];(async()=>{letp1=inquirer.prompt({name: 'boilerplateInfo',type: 'list',message: 'Please select a boilerplate type',choices: choices2,pageSize: choices2.length,});//it will blocksetTimeout(()=>{process.stdin.emit('keypress','\r')},100)awaitp1awaitinquirer.prompt({name: 'boilerplateInfo',type: 'list',message: 'Please select a boilerplate type',choices: choices2,pageSize: choices2.length,});})()
But when we want to simulate user input, it block. I find some solution, but i don't know the reason.
My problem is what's the right way to simulate user input ?
And I find some strange problems with readline.close .Maybe these are node bugs. Inquirer prompt use different readline instance to hanlde questions. I know you intend refactor inquirer . Consider if use one readline instance ? @SBoudrias
solution one:resume process.stdin after close and wait a little time
constinquirer=require('inquirer');constinquirer=require('inquirer');constchoices=[{name: "test1",value: "test1"},{name: "test2",value: "test2"}];constchoices2=[{name: "test1",value: "test1"},{name: "test2",value: "test2"}];functionsleep(time){returnnewPromise(resolve=>{setTimeout(resolve,time);});}(async()=>{letp1=inquirer.prompt({name: 'boilerplateInfo',type: 'list',message: 'Please select a boilerplate type',choices: choices2,pageSize: choices2.length,});setTimeout(()=>{p1.ui.rl.input.emit('keypress','\r')},100)awaitp1.then(()=>p1.ui.rl.input.resume())awaitsleep(10)//this is neccessaryletp2=inquirer.prompt({name: 'boilerplateInfo',type: 'list',message: 'Please select a boilerplate type',choices: choices2,pageSize: choices2.length,});process.stdin.emit('keypress','\r')})()
Sometimes, we want to simulate user input , for example we need test when we write a npm module.
like following code ,it will block at the second prompt , can't see the second question.
look at source code, when one prompt over, the readline instance will pause process.stdin before close , It works good when it's interacting with the reality user. Moreover pause process.stdin is to fix the similar bug , issues: Make inquirer.prompt() blocking, Fix bug to properly close readline, readline: pause stdin before turning off terminal raw mode
But when we want to simulate user input, it block. I find some solution, but i don't know the reason.
My problem is what's the right way to simulate user input ?
And I find some strange problems with readline.close .Maybe these are node bugs. Inquirer prompt use different readline instance to hanlde questions. I know you intend refactor inquirer . Consider if use one readline instance ? @SBoudrias
The text was updated successfully, but these errors were encountered: