javascript - 使用 Mocha 测试 Promise

标签 javascript node.js unit-testing mocha.js chai

我一直在尝试使用 Mocha 测试以下代码,但我总是遇到错误。

Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test

我要测试的代码如下。

'use strict'
const Promise = require('bluebird');

const successResponse = {status: 'OK'};
const failResponse = {status: 'FAIL'};

function dbStatusSuccess () {
    return new Promise(function(resolve, reject) {
        setTimeout(() => {
            resolve(successResponse); 
        }, 2010);

    });
}

function dbStatusFail () {
    return new Promise(function(resolve, reject) {
        setTimeout(() => {
            reject(failResponse); 
        }, 2000);

    });
}

module.exports = {
    dbStatusSuccess,
    dbStatusFail
} 

这是我的测试。

'use strict'
const Promise = require('bluebird');
const chai = require('chai')
chai.use(require('chai-string'))
chai.use(require('chai-as-promised'));
const expect = chai.expect;
chai.should();


const healthyCheck = require('./healthyCheck');

const resp = {status:'OK'};
const resp2 ={status: 'FAIL'};

 describe('healthy-check end point', () => {

    it('should return successful response when connected to database', () => {

        return healthyCheck.dbStatusSuccess()
                            .then((res) => {
                                console.log(JSON.stringify(res, undefined, 2));
                                return expect(res).to.equal(resp);
                            }).catch( (err) => {
                                console.log(err);
                                return expect(err).to.deep.equal(resp2);
                            });


    });
 });

我还在控制台中收到错误 { AssertionError: expected { status: 'OK' } to equal { status: 'OK' }。我相信这是来自 .catch 函数的 loggin 错误。

编辑 1。 从 dbStatusSuccess 函数中删除了拒绝函数。

问题在于 promise 需要 2 秒才能完成/失败。如果 setTimeout 中设置的时间小于 2 秒,则测试通过。

最佳答案

您测试中的默认超时似乎是 2000 毫秒。您的代码显然需要更长的时间才能完成。因此,您必须提高超时限制。如前所述here你不应该使用箭头函数,这样你就可以安全地访问 this

然后你可以像这样增加你的超时时间:

'use strict'
const Promise = require('bluebird');
const chai = require('chai')
chai.use(require('chai-string'))
chai.use(require('chai-as-promised'));
const expect = chai.expect;
chai.should();


const healthyCheck = require('./healthyCheck');

const resp = {status:'OK'};
const resp2 ={status: 'FAIL'};

 describe('healthy-check end point', () => {

    it('should return successful response when connected to database', function() {
        this.timeout(3000);
        return healthyCheck.dbStatusSuccess()
                            .then((res) => {
                                console.log(JSON.stringify(res, undefined, 2));
                                return expect(res).to.equal(resp);
                            }).catch( (err) => {
                                console.log(err);
                                return expect(err).to.deep.equal(resp2);
                            });


    });
 });

那么您的测试应该会按预期运行。

关于javascript - 使用 Mocha 测试 Promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40781513/

相关文章:

angular - 为使用 mat-autocomplete 的组件编写单元测试

在 QT Creator 中捕获单元测试 - main 的多个定义

php - 我如何模拟一个应该接受数组的方法?

javascript - 获取 parent 的 sibling 号码去它的 child 被点击

javascript - 如何使用 Bluebird 处理被拒绝的 promise 中的资源?

javascript - Highcharts - 图表下的位置标题

node.js - 如何在 package.json 中添加多个 NODE_PATH?

javascript - 从 JSONP 中提取函数的 JSON 参数

javascript - 如何将自动完成值添加到列表中?

javascript - 如何将 pdf 嵌入到 android 浏览器的 html 页面中