-
-
Notifications
You must be signed in to change notification settings - Fork 99
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
assert.throws()
with async argument
#35
Comments
We could change it so that promises are supported. But it might be odd that In the meantime this is what I do: try {
await asyncFnThatThrows();
assert.unreachable('should have thrown');
} catch (err) {
assert.ok('threw error');
assert.instance(err, Error);
// ...
} |
Since it's the only assertion that takes a function to execute (along with Your suggested workaround is good for now though, and I've adopted it. |
Hey, I've played with this throughout the week. TBH I'm not liking this in practice. In order for It would also be unique in that no other The Thanks~! |
Thanks for the consideration. Your findings make sense, and I haven't seen any issues in practice using the |
I personally find the boilerplate a tad heavy for await assert.rejected.instance(asyncFnThatThrows(), Error) |
@lukeed Unless I’m missing something, shouldn’t your example read: try {
await asyncFnThatThrows();
assert.unreachable('should have thrown');
} catch (err) {
if (err instanceof assert.Assertion) {
throw err;
}
assert.instance(err, Error);
// ...
} i.e, insert of asserting ok, we should rethrow in the PS. Thank you for uvu and polka, sade, etc. I’m starting to get the feeling you’re writing the new small web server I’m working on for me for the most part ;) |
Nope, no need to re-throw. My snippet is exactly what I do in about 50 different projects. I mean, if all you're doing is asserting that PS no problem :) |
@lukeed I’m confused: so without the assert in the try block, if It’s possible that I am missing something and/or that I need to step away from the screen and come back to this later :) |
@aral I'm saying that if you only have this: try {
await asyncFnThatThrows();
assert.unreachable('should have thrown');
} catch (err) {
assert.instance(err, Error);
} then yes, this could lead to misleading results since the I'm saying that this snippet shouldn't stand on its own in most cases. Instead, the recommendation is to always assert against properties within the try {
await asyncFnThatThrows();
assert.unreachable('should have thrown');
} catch (err) {
assert.instance(err, Error);
assert.match(err.message, 'something specific');
assert.is(err.code, 'ERROR123');
} In this case, if/when I see your PR, thanks! Will review shortly. |
Adding a reference on the idiom of how to assert an async function throws an error: lukeed#35 (comment)
* Update api.assert.md Adding a reference on the idiom of how to assert an async function throws an error: #35 (comment) * Apply suggestions from code review * Update docs/api.assert.md Co-authored-by: Luke Edwards <luke.edwards05@gmail.com>
Hi 👋,
Is
assert.throws()
currently supposed to work with async functions?For example, this fails:
While this passes:
What is the recommended way to test that an async function throws?
Thought of something like this, but there's no
.pass()
or.fail()
:Edit: currently using this hack:
The text was updated successfully, but these errors were encountered: