How can I use setTimeout() functions within Mocha

2019-02-09 06:57发布

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.

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.)

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

miniTest.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.

smallTest.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);
    });
});

4条回答
叼着烟拽天下
2楼-- · 2019-02-09 07:06

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

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

});
查看更多
ゆ 、 Hurt°
3楼-- · 2019-02-09 07:11

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

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

done();

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

done("Test Failed");
查看更多
家丑人穷心不美
4楼-- · 2019-02-09 07:20

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

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

查看更多
仙女界的扛把子
5楼-- · 2019-02-09 07:28

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

describe('small test', function(){
   it('tiny test case', function(done){
       console.log('waiting 3 seconds');
       setTimeout(function(){
           console.log('waiting over.');
           done();
       }, 3000)
   })
})
查看更多
登录 后发表回答