相关疑难解决方法(0)

NodeJS UnhandledPromiseRejectionWarning

所以,我正在测试一个依赖于事件发射器的组件.为此,我想出了一个使用Promise 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();
  });
});
Run Code Online (Sandbox Code Playgroud)

在控制台上我得到一个'UnhandledPromiseRejectionWarning',即使拒绝函数被调用,因为它立即显示消息'AssertionError:Promise error'

(node:25754)UnhandledPromiseRejectionWarning:未处理的promise拒绝(拒绝id:2):AssertionError:Promise错误:expect {Object(message,showDiff,...)}是假的1)应该转换为正确的事件

然后,在2秒后我得到了

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

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

现在有趣的是,如果我注释掉 …

javascript mocha.js node.js promise chai

113
推荐指数
4
解决办法
30万
查看次数

标签 统计

chai ×1

javascript ×1

mocha.js ×1

node.js ×1

promise ×1