摩卡testing超时,如果超过4个testing一次运行

我有一个我正在用Mochatesting的node.js + express web服务器。 我在testing工具中启动Web服务器,并连接到MongoDB以查找输出:

describe("Api", function() { before(function(done) { // start server using function exported from another js file // connect to mongo db }); after(function(done) { // shut down server // close mongo connection }); beforeEach(function(done) { // empty mongo collection }); describe("Events", function() { it("Test1", ...); it("Test2", ...); it("Test3", ...); it("Test4", ...); it("Test5", ...); }); }); 

如果摩卡一次运行4次以上,则超时:

 4 passing (2s) 1 failing 1) Api Test5: Error: timeout of 2000ms exceeded at null.<anonymous> (C:\Users\<username>\AppData\Roaming\npm\node_modules\moch\lib\runnable.js:165:14) at Timer.listOnTimeout [as ontimeout] (timers.js:110:15) 

如果我跳过5个testing中的任何一个,它会成功通过。 如果我重新sortingtesting(总是最后一个超时),会发生同样的问题。 将testing分成组也不会改变事情。

从戳它,最后的testing请求被发送到Web服务器(使用http模块),但它没有被快递接收。 一些testing提出一个请求,一个以上。 这并不影响我跳过的结果。 我一直无法在摩卡外复制这种行为。

究竟是怎么回事?

有了Mocha,如果你为你的(testing)函数的callback(通常叫做done )声明第一个参数,你必须调用它,否则Mocha会等待它被调用(并最终超时)。 如果你不想在testing中需要它,不要声明:

 it('test1', function(done) { .. // 'done' declared, so call it (eventually, usually when some async action is done) done(); }); it('test2', function() { // not an async test, not declaring 'done', obviously no need to call it }); 

而且,由于您使用的是http ,请尝试增加http.globalAgent.maxSockets (默认为5):

 var http = require('http'); http.globalAgent.maxSockets = 100; 

(我相信0完全closures,但我没有尝试过)。