Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Mocha-用于引发异常的断言函数_Node.js_Async Await_Mocha.js_Bdd_Chai - Fatal编程技术网

Node.js Mocha-用于引发异常的断言函数

Node.js Mocha-用于引发异常的断言函数,node.js,async-await,mocha.js,bdd,chai,Node.js,Async Await,Mocha.js,Bdd,Chai,我正在尝试测试异步函数是否将抛出异常,但我一直收到以下错误: AssertionError:预期[函数]会抛出错误 我正在用摩卡咖啡和柴的断言库 it('Throw an error', async () => { assert.throws(async () => { await retrieveException(); }, Error); const retrieveException = async () => {

我正在尝试测试异步函数是否将抛出异常,但我一直收到以下错误:

AssertionError:预期[函数]会抛出错误

我正在用摩卡咖啡和柴的断言库

it('Throw an error', async () => {
    assert.throws(async () => {
        await retrieveException();
    }, Error);

    const retrieveException = async () => {
        // code snippit
        throw new Error('This is an error');
    }
}
检查抛出的异常、异步性质或两者都有问题吗

工具书类 我已经看到了前面的问题(其中一个答案通过三个不同的库[assert和两个BDD方法]),但我无法找到工作

也帮不了什么忙

from Node.js.

expect().to.throw(Error)
也不会仅适用于同步函数。如果您想使用异步函数实现类似的功能,请查看

例如

import chai,{assert}来自'chai';
承诺从“承诺的柴胡”进口柴胡;
柴。使用(柴);
描述('63511399',()=>{
它('抛出错误',异步()=>{
常量retrieveException=async()=>{
抛出新错误(“这是一个错误”);
};
返回assert.isRejected(retrieveException(),错误);
});
});
单元测试结果:

  63511399
    ✓ Throw an error


  1 passing (29ms)

在节点JS sins v10.0.0中有:

assert.rejects(asyncFn[, error][, message])
用于检查异步函数中的异常

摩卡咖啡的外观如下:

it('should tests some thing..', async function () {
  await assert.rejects(async () => {
    throw new Error('This is an error')
  }, 'Error: This is an error')
})

很抱歉反应太晚,但我终于回到了测试工作中,它工作了。谢谢