使用Mocha/chai进行测试时获取UnhandledPromiseRejectionWarning [英] Getting a UnhandledPromiseRejectionWarning when testing using mocha/chai

查看:83
本文介绍了使用Mocha/chai进行测试时获取UnhandledPromiseRejectionWarning的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在测试一个依赖于事件发射器的组件.为此,我想出了将Promises与Mocha + Chai结合使用的解决方案:

So, I'm testing a component that relies on an event-emitter. To do so I came up with a solution using Promises with Mocha+Chai:

it('should transition with the correct event', (done) => {
  const cFSM = new CharacterFSM({}, emitter, transitions);
  let timeout = null;
  let resolved = false;
  new Promise((resolve, reject) => {
    emitter.once('action', resolve);
    emitter.emit('done', {});
    timeout = setTimeout(() => {
      if (!resolved) {
        reject('Timedout!');
      }
      clearTimeout(timeout);
    }, 100);
  }).then((state) => {
    resolved = true;
    assert(state.action === 'DONE', 'should change state');
    done();
  }).catch((error) => {
    assert.isNotOk(error,'Promise error');
    done();
  });
});

在控制台上,尽管正在调用拒绝函数,但我仍收到"UnhandledPromiseRejectionWarning"消息,因为该函数会立即显示消息"AssertionError:Promise错误"

On the console I'm getting an 'UnhandledPromiseRejectionWarning' even though the reject function is getting called since it instantly shows the message 'AssertionError: Promise error'

(节点:25754)UnhandledPromiseRejectionWarning:未处理的承诺 拒绝(拒绝ID:2):AssertionError:Promise错误:预期 {对象(消息,showDiff等)是虚假的

(node:25754) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): AssertionError: Promise error: expected { Object (message, showDiff, ...) } to be falsy

  1. 应该以正确的事件进行过渡

然后,我2秒钟后得到

错误:超时超过2000毫秒.确保done()回调为 在此测试中被调用.

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

自从执行catch回调以来,这甚至更奇怪(我认为由于某种原因断言失败阻止了其余的执行)

Which is even weirder since the catch callback was executed(I think that for some reason the assert failure prevented the rest of the execution)

现在好笑的是,如果我注释掉assert.isNotOk(error...),则测试运行正常,控制台中没有任何警告.从执行捕获的角度来看,它仍然失败".
但是,尽管如此,我还是无法理解这些错误.有人可以启发我吗?

Now the funny thing, if I comment out the assert.isNotOk(error...) the test runs fine without any warning in the console. It stills 'fails' in the sense that it executes the catch.
But still, I can't understand these errors with promise. Can someone enlighten me?

推荐答案

问题是由以下原因引起的:

The issue is caused by this:

.catch((error) => {
  assert.isNotOk(error,'Promise error');
  done();
});

如果断言失败,它将引发错误.此错误将导致永不调用done(),因为代码在它之前出错了.这就是导致超时的原因.

If the assertion fails, it will throw an error. This error will cause done() never to get called, because the code errored out before it. That's what causes the timeout.

未处理的承诺被拒绝" 也是由失败的断言引起的,因为如果在catch()处理程序中抛出错误,则并且不会出现后续的catch()处理程序,该错误就会被吞没(如本文中所述). UnhandledPromiseRejectionWarning警告正在提醒您这一事实.

The "Unhandled promise rejection" is also caused by the failed assertion, because if an error is thrown in a catch() handler, and there isn't a subsequent catch() handler, the error will get swallowed (as explained in this article). The UnhandledPromiseRejectionWarning warning is alerting you to this fact.

通常,如果要在Mocha中测试基于承诺的代码,则应依靠Mocha本身已经可以处理承诺的事实.您不应该使用done(),而是从测试中返回一个Promise.摩卡咖啡然后会自己捕获任何错误.

In general, if you want to test promise-based code in Mocha, you should rely on the fact that Mocha itself can handle promises already. You shouldn't use done(), but instead, return a promise from your test. Mocha will then catch any errors itself.

赞:

it('should transition with the correct event', () => {
  ...
  return new Promise((resolve, reject) => {
    ...
  }).then((state) => {
    assert(state.action === 'DONE', 'should change state');
  })
  .catch((error) => {
    assert.isNotOk(error,'Promise error');
  });
});

这篇关于使用Mocha/chai进行测试时获取UnhandledPromiseRejectionWarning的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆