错误:超出2000毫秒的超时。对于异步测试和挂钩,请确保“done()” - 如何避免此错误?

问题描述 投票:0回答:1

码:

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(),那么我没有得到错误。另外,我猜第一个是fooStubis的sinon.stub没有被调用。

node.js sinon nock sinon-chai
1个回答
0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.