I want to check if an async function throws using assert.throws from the native assert
module.
I tried with
const test = async () => await aPromi
Since Node.js v10.0, there is assert.rejects which does just that.
async
functions never throw - they return promises that might be rejected.
You cannot use assert.throws
with them. You need to write your own asynchronous assertion:
async function assertThrowsAsynchronously(test, error) {
try {
await test();
} catch(e) {
if (!error || e instanceof error)
return "everything is fine";
}
throw new AssertionError("Missing rejection" + (error ? " with "+error.name : ""));
}
and use it like
return assertThrowsAsynchronously(aPromise);
in an asynchronous test case.