我有一个简单的测试应该失败,但它传递错误。
我的代码:
it('my test', async () => {
const result = await resolvingPromise;
expect(result).to.equal('ok');
async function analyzeData() {
let getData = db.getData(1);
let data = await getData;
expect(data.field).to.equal(null);
}
analyzeData();
});
在这个测试中,首先期望是可以的,但async函数内的期望必须失败但测试返回我传递但我看到了这个错误:
UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): AssertionError: expected Fri, 02 Mar 2018 09:47:06 GMT to equal null
(node:295) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我怎么能这样处理拒绝?或者我需要在测试中更改我的异步功能?这样做的好方法是什么?
在进入下一个测试之前,Mocha将等待从测试中返回的任何解决方案。
使用当前代码,undefined
立即从测试函数返回,这意味着mocha继续前进而不等待analyzeData
承诺解决。这导致成功的测试,然后是稍后的未处理拒绝,而不是等待和测试失败。
it('my test', async function(){
const result = await resolvingPromise;
expect(result).to.equal('ok');
})
it('next test', async function(){
let data = await db.getData(1);
expect(data.field).to.equal(null);
})