UnhandledPromiseRejectionWarning测试承诺拒绝

时间:2018-01-04 21:38:45

标签: javascript node.js promise jasmine jasmine-node

我试图编写一个茉莉花测试,其间谍.and.returnValues承诺列表。前几个承诺是拒绝,最后一个是成功。虽然测试通过正常,但Node抱怨如下:

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): undefined
UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): undefined
UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 4): undefined
PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 2)
PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 3)
PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 4)

我的代码非常直接:我创建了一个间谍,将其注入我的代码,调用我的代码,它将循环调用我的间谍直到它不拒绝,然后监视它被调用了5次。间谍是这样的:

var spy = jasmine.createSpy("spy").and.returnValues(
    Promise.reject(),
    Promise.reject(),
    Promise.reject(),
    Promise.reject(),
    Promise.resolve(true)
);

// Inject the spy...

// This will resolve only when the Promise-returning function it calls resolves without rejecting.
myFunc()   
    .then(() => {
        expect(spy).toHaveBeenCalledTimes(5);
        done();
    })
    .catch();

正在测试的代码在其链接中为空.catch(),以验证我没有在那里造成问题。 AFICT,问题是Node看到我正在做Promise.reject()并且认为这是未处理的,事实上,当它被处理时。

如何正确测试被拒绝的承诺?我觉得我需要像Jasmine那样的this

1 个答案:

答案 0 :(得分:1)

这是因为你在后面的事件队列中捕获被拒绝的promise,而不是在创建promise的同一个调用堆栈中。

解决方法是:

var unsafeReject = p => {
  p.catch(ignore=>ignore);
  return p;
};

var p = Promise.reject("will cause warning");

//will catch reject on queue, not on stack
setTimeout(
  ()=>p.catch(e=>console.log("reject:",e))
);

var q = unsafeReject(Promise.reject("will NOT cause warning"));
setTimeout(
  ()=>q.catch(e=>console.log("reject:",e))
);

相关问题