尝试在Mocha中运行异步测试时出现超时错误

时间:2017-10-22 15:41:52

标签: javascript mysql node.js mocha chai

最近,我决定继续一个旧的nodejs项目。这是一个用Express + MySQL模块实现的小型博客引擎。我使用基于原型的类创建了与数据库集成的不同方法,并将这些方法分配给它(例如insert_post()insert_author()等)。要连接,我必须创建一个实例对象并调用适当的方法(例如mysqlConn.delete_comment())。现在我想使用mocha + chaijs添加一些测试。为简化起见,我只编写了一个spec文件,并需要所有其他测试文件。

setup.js

import MYSQL from '../../../../../app/models/database/MYSQL';
import CONFIG from '../../../../../app/config/config';

const ENV_VARS = CONFIG.testing;

const mysql = new MYSQL({
  host: ENV_VARS.db.host,
  user: ENV_VARS.db.user,
  password: ENV_VARS.db.password,
  database: ENV_VARS.db.database,
});

export default mysql;

MYSQL.spec.js

import mysqlConn from './setup';

describe('MYSQL#', function () {
  describe('authors', function () {
    beforeEach(function () {
      mysqlConn.query('TRUNCATE authors;');
    });

    require('./authors/insert_author');
    require('./authors/select_author');
  });
});

当我尝试测试select_author()方法时,我遇到的问题就出现了。我得到Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

select_author()

import { expect } from 'chai';
import mysqlConn from '../setup';

describe('select_author()', function () {
  beforeEach(function() {
    mysqlConn.query('TRUNCATE authors;');

    mysqlConn.insert_author({
      name: 'admin1',
      email: 'admin1@email.com',
      avatar: 'https://some-img.com/admin1.png',
      description: 'web developer',
      role: 'admin'
    });

    mysqlConn.insert_author({
      name: 'admin2',
      email: 'admin2@email.com',
      avatar: 'https://some-img.com/admin2.png',
      description: 'web designer',
      role: 'admin'
    });

    mysqlConn.insert_author({
      name: 'writer1',
      email: 'writer1@email.com',
      avatar: 'https://some-img.com/writer1.png',
      description: 'blogger',
      role: 'writer'
    });

    mysqlConn.insert_author({
      name: 'writer2',
      email: 'writer2@email.com',
      avatar: 'https://some-img.com/writer2.png',
      description: 'marketer',
      role: 'writer'
    });
  });

  it('should return all authors.',function(done) {
    expect(function() {
      mysqlConn.select_author('*', function(err, res) {
        if (err) throw err;
        done();
      }).not.to.throw();
    });
  });
});

以下是select_author()

的实施
module.exports = {
    /** 
     * Select an author entry.
     * @method select_author
     * @memberof MYSQL#
     * @param {!(string|Object)} select_details - The required properties to select a user.
     * @param {?HandleCallback} callback - The callback that handles the response.
     * @example
     * // gets all authors.
     * mysql.select_author('*');
     */
    select_author: function(select_details, callback) {
        if (!(callback instanceof Function)) callback = function() {};

        if (select_details == '*') {
            this.pool.getConnection(function(err, connection) {
                if (err) {
                    return callback(err);
                }
                connection.query(
                    'SELECT * FROM  authors',
                    function(err, result) {
                        connection.release();
                        if (err) {
                            callback(err);
                        } else {
                            callback(null, result);
                        }
                    }
                );
            });
        }
    }
}

我尝试删除beforeEach()内的author_select()个钩子,但是遇到了同样的错误。请注意,我已经测试了insert_author()。我的测试程序有什么问题会导致这个问题吗?这是mocha的输出:

MYSQL#
    authors
      insert_author()
        ✓ should return error if either `email`, `name`, `avatar`or `description` properties are false values.
        ✓ should return error if `role` property is set to value other than `writer` or `admin`.
        ✓ should return error if `email` is not valid email.
        ✓ should succeed. (2277ms)
      select_author()
        1) should return all authors.

  4 passing (12s)
  1 failing

  1) MYSQL# authors select_author() should return all authors.:
     Error: Timeout of 10000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

注意

  1. 我已经尝试this.timeout()但没有成功。
  2. 该方法在app.js
  3. 中按预期工作
  4. 我在conslole.log个回调中添加了insert_author()个语句。我发现it()beforeEach()挂钩结束之前执行。我不知道它是否与问题有关 - 我已经说过我在没有it()的情况下尝试了beforeEach()并得到了同样的错误。

0 个答案:

没有答案