Oclif prompt testing

痴心易碎 提交于 2020-01-04 02:47:11

问题


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

I'm using the 'fancy-test' and '@oclif/test' test frameworks described here: 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'):

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!


回答1:


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



来源:https://stackoverflow.com/questions/56797392/oclif-prompt-testing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!