在摩卡测试中,调用异步函数时如何避免超时错误:超时超过2000ms

node.js Node.js

LGil

2020-03-23

在我的节点应用程序中,我正在使用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();
        });

    });

});

第3003篇《在摩卡测试中,调用异步函数时如何避免超时错误:超时超过2000ms》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

2个回答
小宇宙 2020.03.23

有点晚了,但是将来有人可以使用...您可以通过以下方法更新package.json中的脚本来增加测试超时:

"scripts": { "test": "test --timeout 10000" //Adjust to a value you need }

使用以下命令运行测试 test

小胖Gil 2020.03.23

我的问题是没有发送回响应,所以它挂了。如果您使用Express,请确保对要测试的路由执行了res.send(data),res.json(data)或您想使用的任何api方法。

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android