尽管使用完成,Mocha 仍超时在 Before 挂钩中调用异步承诺链

Mocha times out calling async promise chain in Before hook despite using done

我 运行 在 node.js 上使用 mocha 和 chai 对 mongoose 数据库进行异步集成测试。大多数 运行 都很好,但对于其中一个,我必须使用 before 钩子执行一些预测试 db-prep。当我在 before hook 中使用 Done 时,Mocha 超时。

我遇到的错误是 "Error: Timeout of 5000ms exceeded. For async tests and hooks, ensure "done()" 被调用;如果返回 Promise,请确保它已解决。(/Users/donfelipe/Sites/Agents/test/agents.js)"

曾尝试将 done() 移动到 promise 链末尾的 finally 块中,但这只会在异步链完成执行之前导致 it 块 运行。有点难过。

/* 
    test case to check that the get function returns
*/
process.env.NODE_ENV = 'test-db';
'use strict'

const mongoose = require("mongoose");
const schemas = require('../app_api/models/schemas');
const agents = require('../app_api/controllers/agents.js');
const lists = require('../app_api/controllers/lists.js');
const server = require('../app.js');
const assert = require('assert')
const config = require('config')

//Require the dev-dependencies
const chai = require('chai');
const chaiHttp = require('chai-http');
const should = chai.should();

chai.use(chaiHttp);

describe('Agents test Suite', () => {
    beforeEach(() => {
        //don't have anything too do in this case
    })


    describe('/GET listAddItem suite', function () {

        //before we can test deletion we have to add at least one list in.
        const userModel = mongoose.model('User', schemas.userSchema);
        const agentModel = mongoose.model('Agent', schemas.agentSchema);
        let agentToAddId = {}
        const listObject = {
            listName: 'testList',
            _id: mongoose.Types.ObjectId(),
            agents: [{
                _id: "00000000000000000000000",
                name: "Test agent"
            }]
        }
        const lengthToTestAgainst = listObject.agents.length

        beforeEach((done) => {
             userModel.findById(config.defaultUserId)
                .select('agentList')
                .then((parentDoc) => {
                    if (!parentDoc) {
                        console.error('Unable to find user');
                    }
                    parentDoc.agentList.push(listObject)
                    return parentDoc.save()
                })
                .then(() => {
                    return agentModel.find({})
                })
                .then((response) => {
                    agentToAddId = response[0]._id
                    //console.log(response[0]._id);
                    done()
                })
        })

        it('should add a new item into the testList', (done) => {
            chai.request(server)
                .get(`/agents_api/listAddItem/${config.defaultUserId}/${listObject._id}/${agentToAddId}`)
                .end((err, response) => {
                    response.should.have.status(200)
                    response.body.agentList.testList.should.not.be.equal(lengthToTestAgainst + 1)
                    done(err)
                })

        })
    })

})

呃。我自己解决了这个问题。尾巴摇狗的真实案例。 所以我模拟了 chai.request(服务器)正在对 API 进行的调用:

/agents_api/listAddItem/${config.defaultUserId}/${listObject._id}/${agentToAddId}

并通过POSTMAN提交。原来 API 中有一个错误,所以它没有返回响应,所以我从摩卡获得的超时是一个有效的响应,请求只是挂起。