【问题标题】:Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" - How to avoid this error?错误:超过 2000 毫秒的超时。对于异步测试和钩子,确保“done()”——如何避免这个错误?
【发布时间】:2019-06-24 10:00:49
【问题描述】:

代码:

var processFooBar = function (message, callback) {

  doFooAndBar(message, callback);

};
module.exports.processFooBar = processFooBar;


var doFooAndBar = function (data, callback) {
    async.parallel(
        [
            function (callback) {
                foo(data, function (err, response) {
                    callback(err, response);
                });
            },
            function (callback) {
                bar(data, function (err, response){
                    callback(err, response);
                });
            }
        ],
        function (err, results) {
            callback(err, results);
        }
    );
};
module.exports.doFooBar = doFooBar;

单元测试

describe('Process data', function () {
    var fooStub;

    beforeEach(function (done) {
        done();
    });

    afterEach(function (done) {
        fooStub.restore();
        done();
    });

    it('can process data', function (done) {
        fooStub = sinon.stub(fileName, 'foo').yields(null, null);
        barNockCall();
        app.processFooBar(message,
            function (err, response) {
                nock.isDone().should.be.true;
                nock.cleanAll();
                done();
            }
        }
    });

我收到以下错误:

can process data:
  Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. 
(/Path/To/Test.js)

如果我在 async.parallel 中删除 foo(),则不会收到错误消息。另外,我猜第一个 sinon.stub 是 fooStubis 没有被调用。

【问题讨论】:

标签: node.js sinon nock sinon-chai


【解决方案1】:

您需要增加测试框架的超时时间。它可能有 2000 毫秒的默认超时时间,如果请求时间超过 2 秒,则会抛出错误。

beforeEach(function (done) {
 this.timeout(10000)
 done();
});

覆盖默认超时可能适用于您的情况。

【讨论】:

    猜你喜欢
    • 2019-07-24
    • 2018-07-02
    • 2020-10-23
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    • 2018-06-07
    • 2018-08-09
    • 2019-06-14
    相关资源
    最近更新 更多