node 10 and newer
Since Node.js v10.0, there is assert.rejects
which does just that.
Older versions of node
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.