简体   繁体   English

Oclif 提示测试

[英]Oclif prompt testing

I'm attempting to write a unit test for an Oclif hook that contains a simple prompt.我正在尝试为包含简单提示的 Oclif 挂钩编写单元测试。 I want to test the output of the hook, given a 'Y' or 'N' response to the prompt.我想测试钩子的输出,给出对提示的“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

I'm using the 'fancy-test' and '@oclif/test' test frameworks described here: https://oclif.io/docs/testing我正在使用此处描述的“fancy-test”和“@oclif/test”测试框架: https ://oclif.io/docs/testing

I have tried stubbing the prompt and simulating stdin but neither are working - either the stubbed function is not available or the output is an empty string.我试过存根提示和模拟标准输入,但都没有工作 - 存根函数不可用或输出是空字符串。

Here's an attempt at one test (doesn't work because 'cli.prompt is not a function'):这是一个测试的尝试(不起作用,因为“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() })

It occurred to me that I'm probably not structuring my test properly.我突然想到我可能没有正确构建我的测试。 If anyone could point me in the right direction or provide some pseudo / sample code as to how to approach testing the above hook that would be amazing - thanks!如果有人能指出我正确的方向或提供一些关于如何测试上述钩子的伪/示例代码,那就太棒了 - 谢谢!

Have you tried with:您是否尝试过:

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', () => async () => 'Y')
  .stdout()
  .hook('init')
  .do(output => expect(output.stdout).to.contain('yes'))
  .it()
})

Stubbing with .stub(cli, 'prompt', () => async () => 'Y') worked for me.stub(cli, 'prompt', () => async () => 'Y')存根对我.stub(cli, 'prompt', () => async () => 'Y')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM