-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
'throws' doesn't work with async functions #33
Comments
ospec will see a promise rejection, not a thrown exception, and you can't implicitly await within functions. (If you could, this wouldn't even be needed - just do the await within the inner function if you need to await a promise.) I'll consider this a feature request/gotcha, not a bug. |
Then let me add some motivation to this feature request :) Currently, the best way I know to assert that a given error is thrown is like this: let error
try{
await myFunction()
}catch(e){
error = e
}
o(error.constructor).equals(MyError) which looks really similar to the code that motivated the PR for throws/notThrows in the first place, except that it has an Of course, if we don't want to use let error
myFunction().catch(e => {
error = e
}).finally(() => {
o(error.constructor).equals(MyError)
done()
}) I'd argue that this looks even more convoluted. Compare to o(myFunction).throws(MyError)
// or even
o(async () => myFunction(foo)).throws(MyError) These look much more readable to me. Basically, the absence of this feature forces |
Hello @vitoreiji this may prove useful. asyncNotThrows = function(x) {
return new Promise((f)=>{
x()
.then(
v=>o().equals(),
e => o().satisfies(()=>({
pass: false,
message:
`${x.toString()} shouldn't have thrown
${o.cleanStackTrace(e)}
`
})))
.finally(f)
})
}
o("My async test", async function(){
await asyncNotThrows(async ()=>{throw new Error("Caught by ospec !")})
}) |
The |
ospec version: 4.1.1
node version: 14.15.4
throws
does not seem to catch exceptions fromasync
functions.Code
Expected behavior
Current behavior
The text was updated successfully, but these errors were encountered: