干得好: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函数不可能绑定到回调函数的这一部分并提供超时函数。
底线:对于需要增加超时的函数,不要使用箭头函数。