摩卡不会失败测试
Mocha not fail test
我有一个应该失败的简单测试,但它通过了一个错误。
我的代码:
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();
});
在这个测试中,首先 expect 是可以的,但是 async 函数中的 expect 必须失败但是测试 returns 我已经通过了,但是我看到了这个错误:
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
promise 解析。这会导致测试成功,然后在稍后发生未处理的拒绝,而不是等待和测试失败。
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);
})
我有一个应该失败的简单测试,但它通过了一个错误。
我的代码:
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();
});
在这个测试中,首先 expect 是可以的,但是 async 函数中的 expect 必须失败但是测试 returns 我已经通过了,但是我看到了这个错误:
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
promise 解析。这会导致测试成功,然后在稍后发生未处理的拒绝,而不是等待和测试失败。
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);
})