在摩卡测试中,调用异步函数时如何避免超时错误:超过2000ms的超时 [英] In mocha testing while calling asynchronous function how to avoid the timeout Error: timeout of 2000ms exceeded

查看:78
本文介绍了在摩卡测试中,调用异步函数时如何避免超时错误:超过2000ms的超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的节点应用程序中,我正在使用mocha来测试我的代码.使用mocha调用许多异步函数时,出现超时错误(Error: timeout of 2000ms exceeded.).我该如何解决?

In my node application I'm using mocha to test my code. While calling many asynchronous functions using mocha, I'm getting timeout error (Error: timeout of 2000ms exceeded.). How can I resolve this?

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();
        });

    });

});

推荐答案

您可以在运行测试时设置超时时间:

You can either set the timeout when running your test:

mocha --timeout 15000

或者您可以通过编程设置每个套件或每个测试的超时时间:

Or you can set the timeout for each suite or each test programmatically:

describe('...', function(){
  this.timeout(15000);

  it('...', function(done){
    this.timeout(15000);
    setTimeout(done, 15000);
  });
});

有关更多信息,请参见文档.

For more info see the docs.

这篇关于在摩卡测试中,调用异步函数时如何避免超时错误:超过2000ms的超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆