Replies: 1 comment
-
I have found a way to test multiple calls to Let's suppose we have a single-command-cli import {Command, Flags, ux} from '@oclif/core'
export default class Hello extends Command {
static description = 'describe the command here'
static examples = [
`$ myplugin hello
hello world from ./src/hello.ts!
`,
]
static flags = {
help: Flags.help({char: 'h'}),
// add --version flag to show CLI version
version: Flags.version({char: 'v'}),
}
interactive = async () => {
const object = {
email: '',
name: '',
phone: '',
}
const name = await ux.prompt('What is your name?')
const email = await ux.prompt('What is your email?')
const phone = await ux.prompt('What is your phone number?')
object.name = name
object.email = email
object.phone = phone
return object
}
async run(): Promise<void> {
const object = await this.interactive()
console.log(`Hello ${object.name}!`)
console.log(`Your email is ${object.email}`)
console.log(`Your phone number is ${object.phone}`)
}
} Our import {ux} from '@oclif/core'
import {expect, test} from '@oclif/test'
import Hello from '../src/index'
const promptStub = (stub) => {
stub.onFirstCall().resolves('John Doe') // Alias for stub.onCall(0).resolves('John Doe')
stub.onSecondCall().resolves('[email protected]') // Alias for stub.onCall(1).resolves('[email protected]')
stub.onThirdCall().resolves('123-456-7890') // Alias for stub.onCall(2).resolves('123-456-7890')
return stub
}
describe('basic usage', () => {
test
.stdout({print: true})
.stub(ux, 'prompt', promptStub)
.do(() => Hello.run([]))
.it('runs hello', (ctx) => {
expect(ctx.stdout).to.contain('Hello John Doe!')
expect(ctx.stdout).to.contain('Your email is [email protected]')
expect(ctx.stdout).to.contain('Your phone number is 123-456-7890')
})
})
We need understand that In addition, to test multiple-command-cli's just replace |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm having a hard time writing a unit test for a prompt inside an oclif hook. Is there any additional documentation or samples for this library that would cover this scenario? Thanks!
See:
https://stackoverflow.com/questions/56797392/oclif-prompt-testing
Beta Was this translation helpful? Give feedback.
All reactions