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
Is your feature request related to a problem? Please describe.
I'm developing a highly-interactive CLI application with oclif that utilises inquirer to perform setup with interaction from the user.
I'd like to test my CLI from the perspective of a potential user, exploring the results from answering these questions.
Without direct support for .stdin() in v4, it isn't clear to me how to achieve this with mock-stdin as suggested in the v4 migration docs.
Describe the solution you'd like
An example of how to use v4 to run tests against interactive CLIs.
Describe alternatives you've considered
Revert to v3
Test oclif and inquirer separately, implementing stubs to skip questions when testing oclif and using @inquirer/testing to render and test the outputs when testing interaction. I suspect this will be the suggested alternative, but I feel that it would result in not-particularly-representative tests for my application.
Additional context
Here's some code examples of what I'm working with currently:
test/commands/login.test.ts
import{runCommand}from'@oclif/test'import{expect}from'chai'import{dirname,join}from'node:path'import{describe}from'node:test'import{fileURLToPath}from'node:url'// import { render } from '@inquirer/testing'import{stdinasfstdin}from'mock-stdin'const_dirname=dirname(fileURLToPath(import.meta.url))constroot=join(_dirname,'..','..')conststdin=fstdin()describe('command:init',()=>{it('runs login',async()=>{const{ error, result, stderr, stdout }=awaitrunCommand('login',{ root })// of course, this won't work because the test fails before this due to it hanging waiting for user inputstdin.send('\n')expect(result).to.equal(true)})})
commands/login.ts
import{Command}from'@oclif/core'import{confirm}from'@inquirer/prompts'exportclassLoginextendsCommand{staticdescription='Placeholder'asyncrun(): Promise<boolean>{constresult=awaitconfirm({message: 'Does this work?'})returnresult}}
The text was updated successfully, but these errors were encountered:
To make your test work you should use captureOutput instead of runCommand. This will allow you to send the stdin using mock-stdin before running the command:
Test oclif and inquirer separately, implementing stubs to skip questions when testing oclif and using @inquirer/testing to render and test the outputs when testing interaction. I suspect this will be the suggested alternative, but I feel that it would result in not-particularly-representative tests for my application.
I think this is the best approach to be honest (and is really the only approach if you have more elaborate prompts that a single confirmation). But for single prompts, I think the example above would suffice for most people
Is your feature request related to a problem? Please describe.
I'm developing a highly-interactive CLI application with oclif that utilises inquirer to perform setup with interaction from the user.
I'd like to test my CLI from the perspective of a potential user, exploring the results from answering these questions.
Without direct support for
.stdin()
in v4, it isn't clear to me how to achieve this with mock-stdin as suggested in the v4 migration docs.Describe the solution you'd like
An example of how to use v4 to run tests against interactive CLIs.
Describe alternatives you've considered
Additional context
Here's some code examples of what I'm working with currently:
test/commands/login.test.ts
commands/login.ts
The text was updated successfully, but these errors were encountered: