承诺拒绝不在回拨里面开玩笑

我试图提高现有项目的testing覆盖率,尝试探索笑话的嘲笑(说实话,我对NodeJS来说是一个非常新的东西,来自Java背景),并且认为我对它有基本的了解。 然而,我遇到了一个问题,我仍然无法弄清楚为什么笑话是这样的。

我正在编写testing的示例实现(handler.js):

'use strict'; const profileStore = require('./store/profile-store'); module.exports.invoke = (event, context, callback) => { let id = context.id; // profileStore.read returns a Promise profileStore.read(id) .then((profile) => onSuccess(profile)) .catch((err) => onErrorProfile(err)); function onSuccess(profile) { callback(null, 'Found Profile: ' + profile.name); }; function onErrorProfile(err) { callback(null, 'Error finding Profile'); }; }; 

testing文件(handler.test.js)

 const handlerImp = require('./handler'); let event = {}; const profileStore = require('./stores/profile-store'); jest.mock('./stores/profile-store'); test('1 - should expect error with missing profile', () => { profileStore.read.mockImplementationOnce(() => Promise.reject('for some unexpected reason')); function callback(err, resp) { expect(resp).toEqual('Error finding Profile'); } return handlerImp.invoke(event, null, callback) }); test('2 - should expect result when profile is found', () => { profileStore.read.mockImplementationOnce(() => Promise.resolve({name: 'Random Profile Name'})); function callback(err, resp) { expect(resp).toEqual('Found Profile: Random Profile Name'); } return handlerImp.invoke(event, null, callback) }); test('3 - should fail test when error messages mis-matched', () => { profileStore.read.mockImplementationOnce(() => Promise.reject('for some unexpected reason')); function callback(err, resp) { expect(resp).toEqual('Unable to find Profile'); } return handlerImp.invoke(event, null, callback) }); 

从testing#1和#2我很确定testing确认了快乐的path和exception的情况下正确的。

然而,在testing#3中,即使玩笑发现了错误的期望,testing仍被视为PASS,同样令人费解的是UnhandledPromiseRejectionWarning,本站的一些post指出了代码中的潜在错误,但是我很漂亮确定我的另一个testing用例证实了Promise.reject正在被捕获并正在onErrorProfile()中处理。

(节点:87160)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:3):错误:expect(收到).toEqual(预期)

预期值等于:

“无法findconfiguration文件”

收稿date:

“findconfiguration文件错误”

PASS handler.test.js

✓应该期望错过configuration文件(6毫秒)

✓发现configuration文件时应该预期结果(1ms)

✓错误信息匹配时应该失败testing(3ms)

testing套件:1通过,1总共testing:3通过,3共有快照:0共计时间:0.791s,估计1s

希望有人能指出我错过了什么。 在此先感谢您的帮助。