javascript - 每次失败之前Mocha mysql knex : Can't take lock to run migrations

标签 javascript mysql mocha.js knex.js

这是我的测试设置代码

const knex = require('../db').knex

beforeEach(() => knex.migrate.rollback()
  .then(() => knex.migrate.latest())
  .then(() => knex.seed.run())
)

afterEach(() => knex.migrate.rollback()
  .then(() => {})
)

出现以下错误

Knex:warning - Can't take lock to run migrations: Migration table is already locked
Knex:warning - If you are sure migrations are not running you can release the lock manually by deleting all the rows from migrations lock table: knex_migrations_lock
Unhandled rejection MigrationLocked: Migration table is already locked

      1) "before each" hook for "is not allowed"
Knex:warning - Can't take lock to run migrations: Migration table is already locked
Knex:warning - If you are sure migrations are not running you can release the lock manually by deleting all the rows from migrations lock table: knex_migrations_lock
      2) "after each" hook for "is not allowed"

这里是db.js

const Knex = require('knex')
const Bookshelf = require('bookshelf')
const config = require('config')

var bookshelf = null
var knex = null

exports.init = () => {
  knex = Knex(config.get('database'))
  if (process.env.NODE_ENV !== 'test') {
    knex.migrate.latest()
  }

  bookshelf = Bookshelf(knex)
  bookshelf.plugin('registry')
  bookshelf.plugin('pagination')
  bookshelf.plugin('bookshelf-camelcase')
  bookshelf.plugin('visibility')

  exports.bookshelf = bookshelf
  exports.knex = knex

}

mocha.opts

--ui bdd
--slow 70
--growl
--recursive
--reporter spec

最佳答案

事实证明,由于 mysql,beforeEach 钩子(Hook)花费了很长时间。

使用 this.timeout 为我解决了这个问题!

beforeEach(async function () {
  this.timeout(60 * 1000)
  await knex.migrate.rollback()
  await knex.migrate.latest()
  return knex.seed.run()
})

关于javascript - 每次失败之前Mocha mysql knex : Can't take lock to run migrations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48213015/

相关文章:

javascript - 使用组件 Angular - TypeScript 处理表单错误

php - 我的变量为零但 IF 无法识别它?

mysql - 如何像这样选择2个表

javascript - Mocha 测试的 Istanbul 尔代码覆盖率

javascript - JSON.parse 不将字符串转换为对象

javascript - 前置内容

javascript - React Mocha-chai 测试无法识别 Prop 中的商店

node.js - 单独运行 Mocha 测试

javascript - 无法在 IE9 及更高版本中从 HyperLink 控件打开文件

php - YII 添加外键问题