簡體   English   中英

Oclif 提示測試

[英]Oclif prompt testing

我正在嘗試為包含簡單提示的 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

我正在使用此處描述的“fancy-test”和“@oclif/test”測試框架: https ://oclif.io/docs/testing

我試過存根提示和模擬標准輸入,但都沒有工作 - 存根函數不可用或輸出是空字符串。

這是一個測試的嘗試(不起作用,因為“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() })

我突然想到我可能沒有正確構建我的測試。 如果有人能指出我正確的方向或提供一些關於如何測試上述鈎子的偽/示例代碼,那就太棒了 - 謝謝!

您是否嘗試過:

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', () => async () => 'Y')存根對我.stub(cli, 'prompt', () => async () => 'Y')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM