javascript 如何在 Mocha 测试用例中使用 setTimeout() 函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32730169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:41:43  来源:igfitidea点击:

How can I use setTimeout() functions within Mocha test cases?

javascriptnode.jsmocha

提问by nyarasha

I'm writing a test in Mocha / Node js and want to use a setTimeout to wait for a period before executing a block of code.

我正在用 Mocha/Node js 编写测试,并希望使用 setTimeout 在执行代码块之前等待一段时间。

How can I accomplish this?

我怎样才能做到这一点?

It appears that within a Mocha test case, setTimeout() does not work. (I am aware that you can setTimeout per test case and per test file, that's not what I need here.)

似乎在 Mocha 测试用例中, setTimeout() 不起作用。(我知道您可以为每个测试用例和每个测试文件设置超时,这不是我在这里需要的。)

In a js file run with Node, ie, node miniTest.js, this will wait 3 seconds, then print the line inside the setTimeout function.

在使用 Node 运行的 js 文件中,即 ,node miniTest.js这将等待 3 秒,然后在 setTimeout 函数中打印该行。

miniTest.js

小测试.js

console.log('waiting 3 seconds...');
setTimeout(function() {
    console.log('waiting over.');
}, 3000);

In a js file run with Mocha, ie, mocha smallTest.js, this will not wait, and will finish executing and exit without ever printing the line inside the setTimeout function.

在使用 Mocha 运行的 js 文件中,即 ,mocha smallTest.js这不会等待,并且将完成执行并退出而不会在 setTimeout 函数中打印该行。

smallTest.js:

小测试.js:

mocha = require('mocha');

describe('small test', function() {
    it('tiny test case', function() {
        console.log('waiting 3 seconds...');
        setTimeout(function () {
            console.log('waiting over.')
        }, 3000);
    });
});

回答by Neelabh Singh

You are forgetting to pass parameter in it('tiny test case', function()and call done() after console.log in setTimeout method.

您忘记it('tiny test case', function()在 setTimeout 方法中的 console.log 之后传入参数并调用 done() 。

describe('small test', function(){
   it('tiny test case', function(done){
       console.log('waiting 3 seconds');
       setTimeout(function(){
           console.log('waiting over.');
           done();
       }, 3000)
   })
})

回答by JMM

Have your test function take a parameter (typically called done). Mocha will pass a function that you'll call in the setTimeoutfunction (e.g. after console.logcall done()).

让您的测试函数接受一个参数(通常称为done)。Mocha 将传递一个您将在setTimeout函数中调用的函数(例如 after console.logcall done())。

See https://mochajs.org/#asynchronous-code.

请参阅https://mochajs.org/#asynchronous-code

回答by Vipul

You need to have done passed as a parameter in your test which will be invoked once the test passes.

您需要在测试中将 done 作为参数传递,一旦测试通过,就会调用该参数。

You can write your test like

你可以像这样写你的测试

mocha = require('mocha');

describe('small test', function(done) {
    it('tiny test case', function() {
       console.log('waiting 3 seconds...');
       setTimeout(function () {
           console.log('waiting over.');
           done();
       }, 3000);
    });

});

});

This will wait 3 seconds after that it will print 'waiting over' and pass the test. You can also have a condition inside the timeout depending upon whether the condition is satisfied or not you can pass the test by calling

这将等待 3 秒钟,然后它将打印“等待结束”并通过测试。您还可以在超时内设置条件,具体取决于条件是否满足您可以通过调用通过测试

done();

or fail the test by either throwing an error or passing the error message in

或者通过抛出错误或传递错误消息来使测试失败

done("Test Failed");

回答by Ska

This is a complete example. You need to call done() in every assertion you make. You can leave out the beforefunction and do the setTimeout in one of your itfunctions, but it should still use and call done() after assert.

这是一个完整的例子。您需要在所做的每个断言中调用 done() 。您可以省略before函数并在其中一个it函数中执行 setTimeout ,但它仍应在断言后使用并调用 done() 。

var foo = 1;
before(function(done) {
  setTimeout(function(){
    foo = 2;
    done();
  }, 500)
});

describe("Async setup", function(done){

  it("should have foo equal to 2.", function(done){
    expect(foo).to.be.equal(2);
    done();
  });

  it("should have foo not equal 3.", function(done){
    expect(foo).to.be.not.equal(3);
    done();
  });

});