简体   繁体   English

使用 mocha.js 测试 CLI 应用程序

[英]Testing CLI app with mocha.js

I want to test a CLI app and want to receive user input while the test is running, since mocha.js has 200ms limit the test is failing.我想测试 CLI 应用程序并希望在测试运行时接收用户输入,因为 mocha.js 有 200 毫秒的限制,测试失败。 So I want to know how to halt the test while the script is getting input and resume after getting it.所以我想知道如何在脚本获取输入时停止测试并在获取它后恢复。

You can increase test timeouts like so;您可以像这样增加测试超时;

describe('increased timeout', function(){
  it('should not timeout', function(done){
    this.timeout(2000);
    setTimout(done, 1500);
  });
});

But I don't recommend you do this.但我不建议你这样做。 You can use nexpect or stream-expect to simulate user input.您可以使用nexpectstream-expect来模拟用户输入。 This way you can write alot of automated tests.通过这种方式,您可以编写大量自动化测试。

Example usage of stream-expect 流期望的示例用法

您的测试运行器可以添加超时参数,类似于此。

mocha -r ts-node/register test/index.ts **--timeout 15000** --reporter mochawesome

You should make your test async.你应该让你的测试异步。 Add done argument and invoke it when you done.添加done参数并在完成后调用它。 http://visionmedia.github.io/mocha/ - check Asynchronous code section for samples. http://visionmedia.github.io/mocha/ - 检查示例的异步代码部分。

http://jsfiddle.net/iskomorokh/3jnoyovb/9/ http://jsfiddle.net/iskomorokh/3jnoyovb/9/

@UPDATE based on the feedback: @UPDATE基于反馈:

Timeouts can be disabled this way:可以通过以下方式禁用超时:

this.enableTimeouts(false);

And here is a link to docs这是文档的链接

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

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