Oclif即时测试 [英] Oclif prompt testing

查看:108
本文介绍了Oclif即时测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为包含简单提示的Oclif挂钩编写单元测试。考虑到提示为 Y或 N,我想测试挂钩的输出。

I'm attempting to write a unit test for an Oclif hook that contains a simple prompt. I want to test the output of the hook, given a 'Y' or 'N' response to the prompt.

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

我正在使用'fancy -test和 @ oclif / test测试框架,如下所示:
https://oclif.io/ docs / testing

I'm using the 'fancy-test' and '@oclif/test' test frameworks described here: https://oclif.io/docs/testing

我尝试过对提示进行存根并模拟stdin,但均不起作用-存根函数不可用或输出为空字符串。

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.

这里是一次测试(因为'cli.prompt不是函数',所以不起作用):

Here's an attempt at one test (doesn't work because 'cli.prompt is not a function'):

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!

推荐答案

您是否尝试过:

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()
})

.stub(cli,'prompt',()=>异步()=>'Y')对我有用

这篇关于Oclif即时测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆