使用knex和mocha进行测试

问题描述 投票:0回答:1

所以我使用带有Node的Knex模块来连接我的数据库,这很棒,做我需要的一切。

但是,当我在mocha中运行我的单元测试时,即使单元测试完成,该过程仍继续运行。

所以我看到其他人有一个类似的问题,它永远在运行,他们的解决方案是使用knex.destroy()。我尝试了这个,它适用于一个文件,但我运行我的单元测试,如npm run test *,它将运行任何文件..所以一旦第一个测试运行销毁,以下测试然后都失败。

我只是想知道这是否仍然在运行尚未解决的承诺?或配置错误的knex?

我这样配置knex ..

'use strict';
const
    config = require('../../config/config');

module.exports = require('knex')({
    client: 'mysql',
    connection: {
        host: config.get('db.host'),
        user: config.get('db.user'),
        password: config.get('db.password'),
        database: config.get('db.database'),
        multipleStatements: true
    },
    pool: {
        min: 0,
        max: 5,
    // debug: true
});

当我需要一个数据库项目时,我只需用db.insert(...)等来调用它。

感觉有点不对,我不需要获得Knex的实例?

希望有人可以给我一些关于它的指示吗?

提前致谢。格兰特

unit-testing mocha forever
1个回答
0
投票

我和knex 0.15.2,pg 7.4.3和mocha 5.2.0有同样的问题。在每种测试方法之后解决它像knex.destroy()推荐的那样:

let knex: Knex
beforeEach(async () => {
  knex = Knex({
    client: 'pg',
    connection: config.postgres.connection_string,
    debug: config.postgres.debug,
    acquireConnectionTimeout: config.postgres.connection_timeout
  })
})    
afterEach(async () => {
  await knex.destroy()
})
© www.soinside.com 2019 - 2024. All rights reserved.