对于 Promise 中抛出的错误,Express Supertest 总是给出超时错误

Jac*_* Ye 6 mocha.js node.js express supertest es6-promise

假设我有一些控制器逻辑:

// registered for route POST /login
function login(req, res, next) {
  User.findOne({username: req.body.username}).exec()
    .then(user => {
      if (user) { 
        return bcrypt.compareAsync(req.body.password, user.password) 
      } else { 
        throw new APIError("Unauthorized", 401) 
      }
    })
    .then(some more chained promises)
    .catch(next)
}
Run Code Online (Sandbox Code Playgroud)
  1. 该路线在开发服务器中运行良好。
  2. 我已经实现了错误处理中间件。401 错误可以正确传播到中间件并给出 401 响应。
  3. 我使用Mocha + Supertest(当我需要存根一些方法时使用Sinon + SinonTest)进行测试。
  4. 快乐之路的考验可以通过。
  5. 在测试中正确检测到控制器之前的其他中间件(例如快速验证)生成的悲伤路径的测试。

但是当在控制器中测试悲伤路径时,它总是给出超时错误:

it('throws unauthorized error if no user match', () => {
  return request(app)
    .post('/login')
    .send({ username: 'notExistInDB', password: 'asdfasd'})
    .expect(401)
})
Run Code Online (Sandbox Code Playgroud)

此测试失败并显示错误消息:

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
Run Code Online (Sandbox Code Playgroud)

到目前为止我已经尝试过但没有成功的事情:

  1. 使用 Mocha 的done()回调而不是返回 Supertest 承诺
  2. 存根所有异步调用以立即返回
  3. 增加超时时间

如果您对可能出现的问题有任何线索,请提供帮助。这真的让我很烦恼,因为我无法测试控制器中的任何悲伤路径,并且当测试失败时我永远看不到实际的错误。