提问者:小点点

如何在mocha中增加单个测试用例的超时


我在测试用例中提交了一个网络请求,但这有时需要超过2秒(默认超时)。

如何增加单个测试用例的超时?


共3个答案

匿名用户

干得好:http://mochajs.org/#test-水平仪

it('accesses the network', function(done){
  this.timeout(500);
  [Put network code here, with done() in the callback]
})

对于箭头功能,请按如下方式使用:

it('accesses the network', (done) => {
  [Put network code here, with done() in the callback]
}).timeout(500);

匿名用户

如果希望使用es6箭头函数,可以添加。超时(毫秒)it定义的末尾:

it('should not timeout', (done) => {
    doLongThing().then(() => {
        done();
    });
}).timeout(5000);

至少这在打字稿中是有效的。

匿名用户

(自从我今天遇到这个)

使用ES2015 fat arrow语法时要小心:

这将失败:

it('accesses the network', done => {

  this.timeout(500); // will not work

  // *this* binding refers to parent function scope in fat arrow functions!
  // i.e. the *this* object of the describe function

  done();
});

编辑:为什么失败:

正如@atoth在评论中提到的,胖箭头函数没有自己的此绑定。因此,it函数不可能绑定到回调函数的这一部分并提供超时函数。

底线:对于需要增加超时的函数,不要使用箭头函数。