node.js - Node/Mocha 测试超时

标签 node.js express mocha.js

尽管我尽了最大的努力,但我有一些简单的基于 Promise 的代码仍然超时(> 2000ms)。帮忙?

export function listCurrentUserPermissions(req, res, next) {
  return UserPermission.findAll({
    where: { accountId: req.user.tenant() }
  }).catch((error) => {
    console.log('-----------------------------------');
    console.log(error);
    console.log.bind(console);
    console.log('-----------------------------------');
  }).then((permissions) => {
    return res.json({ userPermission: permissions.map(serializeUserPermission) });
  }, next);

测试:

  describe('GET /api/v0/permissions', () => {
    it('shows the current users permissions', () => {
      return api.listCurrentUserPermissions(req, res, next).then(() => {
        expect(UserPermission.findAll).to.have.been.calledWithMatch({
          where: { accountId: req.user.tenant() }
        });

        expect(next).to.have.beenCalled;
        expect(next.lastCall.args[0].output.payload.statusCode).to.equal(200);
        expect(next.lastCall.args[0].output.payload.permission).to.include(nonAdminPermission.permission);
      });
    });
  })

我得到的错误:

Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

console.log 从未被调用,但我可以确认代码是使用正确的参数调用的,等等。

最佳答案

看起来 next 延续被作为 then 方法的错误处理程序(第二个参数)传递。

我想你想要:

export function listCurrentUserPermissions(req, res, next) {
  return UserPermission.findAll({
    where: { accountId: req.user.tenant() }
  }).catch((error) => {
    console.log('-----------------------------------');
    console.log(error);
    console.log.bind(console);
    console.log('-----------------------------------');
  }).then((permissions) => {
    return res.json({ userPermission: permissions.map(serializeUserPermission) });
  }).then(next);

关于node.js - Node/Mocha 测试超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32230112/

相关文章:

php - 如何在 aws lambda 中将 imagick 与 php 一起使用?

javascript - 切换http状态码

javascript - 如何在部署expressjs应用程序时修复heroku错误?

javascript - Mongoose 类型错误 : object is not a function when instantiating object of a schema type

javascript - Promise.resolve()和resolve()之间的区别?

node.js - 如何将 node_modules 与实际的 package.json 同步?

javascript - ejs错误: Could not find matching close tag for "<%-"

node.js - Mocha + SeleniumJS + PhantomJS 检索超时

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

async-await - mocha async/await 测试因超时而失败