我试图为一个包含简单提示的Oclif挂钩编写单元测试。我想在给出'Y'或'N'响应后测试挂钩的输出。
import {Hook} from '@oclif/config'
import cli from 'cli-ux'
const hook: Hook<'init'> = async function () {
const answer = await cli.prompt("Y or N?")
if(answer === 'Y') {
this.log('yes')
}
else {
this.log('no')
}
}
export default hook
以下是一个测试的尝试(不起作用,因为'cli.prompt不是一个函数'):
import {expect, test} from '@oclif/test'
import cli from 'cli-ux'
import * as sinon from 'sinon';
describe('it should test the "configure telemetry" hook', () => {
test
.stub(cli, 'prompt', sinon.stub().resolves('Y'))
.stdout()
.hook('init')
.do(output => expect(output.stdout).to.contain('yes'))
.it()
})
我意识到我的测试可能没有正确地进行结构化。如果有人能指点我正确的方向或提供一些伪代码/示例代码来测试上述钩子,那就太棒了 - 谢谢!
async (input) => { return 'Y'}
中编写一个函数,并根据提示返回不同的值。 - st.huber