sac*_*hin 190 mocha.js node.js chai
在我的节点应用程序中,我使用mocha来测试我的代码.在使用mocha调用许多异步函数时,我收到超时错误(Error: timeout of 2000ms exceeded.).我该如何解决这个问题?
var module = require('../lib/myModule');
var should = require('chai').should();
describe('Testing Module', function() {
    it('Save Data', function(done) {
        this.timeout(15000);
        var data = {
            a: 'aa',
            b: 'bb'
        };
        module.save(data, function(err, res) {
            should.not.exist(err);
            done();
        });
    });
    it('Get Data By Id', function(done) {
        var id = "28ca9";
        module.get(id, function(err, res) {
            console.log(res);
            should.not.exist(err);
            done();
        });
    });
});
And*_*ren 334
您可以在运行测试时设置超时:
mocha --timeout 15000
或者,您可以以编程方式为每个套件或每个测试设置超时:
describe('...', function(){
  this.timeout(15000);
  it('...', function(done){
    this.timeout(15000);
    setTimeout(done, 15000);
  });
});
有关详细信息,请参阅文档.
oli*_*ren 80
我发现只是增加超时的"解决方案"模糊了这里真正发生的事情,也就是说
当Mocha没有收到来自回调的断言错误时,通常会遇到#2.这是由一些其他代码吞噬堆栈中的异常引起的.处理这个问题的正确方法是修复代码而不是吞下错误.
当外部代码吞噬您的错误时
如果它是一个你无法修改的库函数,你需要捕获断言错误并自己将它传递给Mocha.您可以通过将断言回调包装在try/catch块中并将任何异常传递给done处理程序来完成此操作.
it('should not fail', function (done) { // Pass reference here!
  i_swallow_errors(function (err, result) {
    try { // boilerplate to be able to get the assert failures
      assert.ok(true);
      assert.equal(result, 'bar');
      done();
    } catch (error) {
      done(error);
    }
  });
});
这个样板文件当然可以被提取到一些实用功能中,使测试更加令人赏心悦目:
it('should not fail', function (done) { // Pass reference here!
    i_swallow_errors(handleError(done, function (err, result) {
        assert.equal(result, 'bar');
    }));
});
// reusable boilerplate to be able to get the assert failures
function handleError(done, fn) {
    try { 
        fn();
        done();
    } catch (error) {
        done(error);
    }
}
加速网络测试
除此之外,我建议您接受关于开始使用测试存根进行网络调用的建议,以使测试通过,而不必依赖于正常运行的网络.使用Mocha,Chai和Sinon测试可能看起来像这样
describe('api tests normally involving network calls', function() {
    beforeEach: function () {
        this.xhr = sinon.useFakeXMLHttpRequest();
        var requests = this.requests = [];
        this.xhr.onCreate = function (xhr) {
            requests.push(xhr);
        };
    },
    afterEach: function () {
        this.xhr.restore();
    }
    it("should fetch comments from server", function () {
        var callback = sinon.spy();
        myLib.getCommentsFor("/some/article", callback);
        assertEquals(1, this.requests.length);
        this.requests[0].respond(200, { "Content-Type": "application/json" },
                                 '[{ "id": 12, "comment": "Hey there" }]');
        expect(callback.calledWith([{ id: 12, comment: "Hey there" }])).to.be.true;
    });
});
有关更多信息,请参阅Sinon的nise文档.
luk*_*s_o 17
如果您使用箭头函数:
it('should do something', async () => {
  // do your testing
}).timeout(15000)
小智 7
有点晚了,但是将来有人可以使用...您可以通过以下方法更新package.json中的脚本来增加测试超时:
"scripts": {
     "test": "test --timeout 10000" //Adjust to a value you need
  }
使用以下命令运行测试 test