在official documentation中可以找到。尝试下面的代码只会导致undefined。
assert.throws(
() => {
throw new Error('Wrong value');
},
Error
);我发现了一些关于箭头函数=>在语句中不起作用的问题,但将其更改为function()并不会改变任何事情。在上面的链接中,我的节点版本是完全相同的版本。
我遗漏了什么?
编辑:
console.log(assert.throws(
() => {
throw new Error('Wrong value');
},
Error
)) // this will log "undefined"
let result = assert.throws(
() => {
throw new Error('Wrong value');
},
Error
);
console.log(result); // as well as this非常感谢
发布于 2020-05-04 23:05:13
因此,正如@tkausl在注释中所说的(顺便说一句,谢谢。) assert.throws“断言传递的函数抛出”。
为了达到我想要的结果,我不得不这样做:
console.log(() => {
let error;
try {
// function that may throw an error
} catch (e) {
error = e;
}
// and than do something with 'error' and return a value
return error
}())https://stackoverflow.com/questions/61267617
复制相似问题