失败时,测试不会调用done()

时间:2017-05-05 20:08:37

标签: node.js postgresql unit-testing mocha chai

我正在编写一个单元测试来测试我的postgres架构。我正在使用node-pg,mocha,sinon和chai。

这样做 - 测试通过没有问题:

describe('When adding a user', ()=> {
  it('should reject since email is used somewhere else', (done)=> {
    pool.query(`INSERT INTO users(email, id, token)
               VALUES($1, $2, $3)`, ['foo@email.com', '12346', 'fooToken'])
    .then((result)=> {
      console.log('nothing in here runs, you will not see this');
      done()
    })
    .catch((result) => {
      result.constraint.should.have.string('email_already_exists');
      done();
    })
  })
});

但是为了确保我没有得到误报,我将断言更改为result.constraint.should.not.have.string('email_already_exists');以故意使测试失败。

而不是测试失败,我得到Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

我得到了什么?

1 个答案:

答案 0 :(得分:0)

答案:

node-pg的promise链在测试期间导致了这个奇怪的问题。如果我没有回调,那么没问题:

describe('When adding a user', ()=> {
  it('should reject since email is used somewhere else', (done)=> {
    function callback(err, result) {
      err.constraint.should.not.have.string('email_already_exists');
      done();
    }
    pool.query(`INSERT INTO users(email, id, token)
               VALUES($1, $2, $3)`, ['foo@email.com', '12346', 'fooToken'], callback)
  })
});