Jest无法处理sequelize.sync()承诺

时间:2017-07-15 01:10:48

标签: node.js sequelize.js jestjs

我将此测试文件与Jest一起运行:

test('this works', () => {
  Promise.resolve();
});

test('this also works', (done) => {
  setTimeout(done, 100);
});

test('this does not work', () => {
  return models.sequelize.sync({force: true, logging: false});
});

test('this also does not work', (done) => {
  models.sequelize.sync({force: true, logging: false}).then(() => done());
});

Sequelize.js或Jest有些奇怪。想法?

澄清:我没有得到失败的测试,所有4个测试都是绿色的。后两者将重置数据库,但即使在所有测试运行后,Jest进程也将永远挂起。 Jest会说:4通过了,我只需要用Ctrl + C手动退出。

1 个答案:

答案 0 :(得分:1)

正如本Github问题所述:https://github.com/sequelize/sequelize/issues/7953 sync方法实际上只会在连接最终空闲时返回,因此解决方案是:

models.sequelize.sync({force: true, logging: false}).then(() => {
  models.sequelize.close();
});