【问题标题】:Mocha tests timeout摩卡测试超时
【发布时间】:2019-06-13 02:42:18
【问题描述】:

我有一个注册函数的单元测试。我返回一个承诺,但收到一个错误:

Error: Timeout of 30000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

我尝试过使用 done() 函数、异步等待、检查 Promise 拒绝 - 没有结果。

下面是我的测试代码:

import authController from '../../../src/controllers/authController';
import { expect } from 'chai';
import sinon from 'sinon';

describe('Test register method', () => {
    it('Register method should create new user', () => {
        const req = {
            file: sinon.spy(),
            body: {
                name: "John",
                email: "johndoe@example.com",
                phone: "123",
                gender: "Male",
                birthDate: "12-12-1992",
                purpose: "Friends",
                password: "Secret"
            }
        };
        const res = {
            status: function () {
                return this;
            },
            json: sinon.spy()
        };
        return authController.register(req,res).then(() => {
            expect(res.json.firstCall.lastArg.success).to.equal(true);

        });
    });
});

被测控制器代码:

https://github.com/elszczepano/FindMates-API/blob/master/src/controllers/authController.js

【问题讨论】:

  • 该代码是否在测试之外工作? (或者你能确认register 确实在解决吗?)
  • 是的,当我使用 Postman 并手动测试我的代码时,一切正常。

标签: javascript node.js mocha.js sinon chai


【解决方案1】:

我解决了! 第一的: 我不得不将 package.json 中的超时设置为 10000 毫秒:

"test": "nyc mocha --require @babel/register --require @babel/polyfill tests/unit/**/*.test.js --timeout 10000"

第二: 我必须创建与我的 MongoDB 数据库的连接(当然还有在测试后断开连接)。

before(done =>{
    mongoose.connect('mongodb://localhost:27017/FindMates', {useNewUrlParser: true });
    mongoose.set('useCreateIndex', true);
    mongoose.set('useFindAndModify', false);
    done();
});
after(done => {
    mongoose.disconnect();
    done();
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-13
    • 2015-05-23
    • 2014-03-23
    • 2013-07-11
    • 1970-01-01
    • 2018-02-03
    相关资源
    最近更新 更多