在下一次testing之前让摩卡等待

有一些摩卡testing,需要以前的函数调用数据,但是,因为它使用的是web服务,并希望它在运行下一个testing之前等待一段预定的时间,如下所示:

var global; it('should give some info', function(done) { run.someMethod(param, function(err, result) { global = result.global done(); }); }); wait(30000); // basically block it from running the next assertion it('should give more info', function(done) { run.anotherMethod(global, function(err, result) { expect(result).to.be.an('object'); done(); }); }); 

任何想法,将不胜感激。 谢谢!

setTimeout绝对可以帮助,但可能有一个“干净”的方式来做到这一点。 docs实际上说使用this.timeout(delay)以避免testingasynchronous代码时发生超时错误,所以要小心。

 var global; it('should give some info', function(done) { run.someMethod(param, function(err, result) { global = result.global done(); }); }); it('should give more info', function(done) { this.timeout(30000); setTimeout(function () { run.anotherMethod(global, function(err, result) { expect(result).to.be.an('object'); done(); }); }, 30000); }); 

虽然this.timeout()会延长单个testing的超时时间,但这不是问题的答案。 this.timeout()设置当前testing的超时时间。

不过别担心,反正你应该没问题的。 testing并不是平行的,它们是以串联方式进行的,所以你不应该对你的全局方法有任何问题。