[英]nodejs assert module: assert.throws only returns “undefined”
As found in the official documentation .如官方文档中所见。 Trying the below code would only result in
undefined
.尝试下面的代码只会导致
undefined
。
assert.throws(
() => {
throw new Error('Wrong value');
},
Error
);
I was able to find some issues about the arrow functions =>
not working in the statement but changing that to function()
doesn't change a thing.我能够找到一些关于箭头函数
=>
在语句中不起作用但将其更改为function()
并不会改变任何事情的问题。 As in the link above my node version is the exact same version.如上面的链接所示,我的节点版本是完全相同的版本。
What am I missing?我错过了什么?
EDIT:编辑:
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
Many thanks非常感谢
So, as @tkausl has stated in the comments (thanks by the way.) assert.throws
"asserts that the passed function throws".因此,正如@tkausl在评论中所说(顺便说一句)
assert.throws
“断言传递的 function 抛出”。
In order to achieve my desired result, I had to do this:为了达到我想要的结果,我必须这样做:
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
}())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.