-
Notifications
You must be signed in to change notification settings - Fork 643
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
std assertThrows is acting like assertThrowsError #1362
Comments
We assert that it's an error because it's ergonomic for the callback overload to be able to assume that: https://deno.land/[email protected]/testing/asserts.ts#L615. Names don't need to be 100% precise, |
I think it's ok to allow non-Error thing thrown from the function as it's allowed by the language. I'm not sure we need
Maybe we can check that only when errorCallback is specified, or we can change the errorCallback signature to |
That is a much better solution. We should be able to provide null as second parameter so we can reach the 3rd parameter to set |
Im diving in. After cloning and running Not sure what is going on. The only thing I can think about is: Could this be caused by using a M1 CPU?
All except the first one fails with |
@mathiasrw Have you initialized git-submodules?
|
How about changing assertThrows and assertRejects to only assert that their was an exception thrown or rejected? If those functions were changed to take an optional expectation, these functions could be used for asserting specific exceptions or that their was an exception. Having it return the unknown exception would allow us to make additional assertions on the exception if they are more complex than a simple comparison. export function assertThrows(
fn: () => unknown,
expected?: unknown,
): unknown;
export function assertRejects(
fn: () => unknown,
expected?: unknown,
): unknown; Then to keep the ability for checking errors, we could add assertError as a separate function since it's common for people to want to make assertions about the errors that were thrown or rejected. // with variable
let error = assertThrows(() => func());
assertError(error, CustomError, "Fail");
// one liner
assertError(assertThrows(() => func()), CustomError, "Fail"); An example of what assertError could look like can be found here: #1376 By decoupling assertError from assertThrows and assertRejects, it would also make it easy for people to be able to make assertions about errors that were already caught. Having assertThrows and assertRejects return the exception would also get rid of the need to have an errorCallback option since users could just make additional assertions on the returned value rather than in a callback. Another example I thought of where it would be useful to have assertError as a separate function would be where an error has a cause. MDN shows that you can add an error onto a new error as a cause here. let error = assertThrows(() => func());
assertError(error, CustomError, "Fail");
assertError(error.cause, AnotherCustomError, "Fail"); |
I'm in favor of this change
We had discussion about this, and decided we don't make So I think we should keep the return type void here |
In that case and for backwards compatibility, I think we should keep the current call signature so that their is a shorthand way to make assertions about exceptions being errors(common case), but if user provides a callback instead use it instead of assertError so that users can make their own assertions about the exception. Currently when a callback is passed it asserts the exception is an error and optionally asserts that error has a msg before calling the callback. Since my suggestion is not doing error assertions when passing an assertion callback, I believe the last optional msg arg should be deprecated. I believe assertError should be exposed for if users want to use the callback signature of assertThrows or assertRejects but still want to make the error assertions on it or on error.cause. I would also remove the optional callback signature from my current assertError implementation since the error would be a variable where it would be used. |
That sounds reasonable to me. Maybe Let's first only implement |
Ok. Jumping out of this. Would love to see the change, but not sure I can lift the adjustments suggested. Update Just realised its being solved as part of #1376 - perfect! :) |
My PR doesn't solve this, it will just make this change easier. assertThrows and assertRejects will still do error assertions by calling assertError. If it gets merged, all you would have to do is make assertThrows and assertRejects not call assertError if I think if both those changes were made, |
@KyleJune Ahh, got it. Ill keep an eye on your PR and have a go at this one after the PR has been merged. |
I think #1376 solves the issue presented here in the limit. I'm going to close the issue for now, please feel free to comment if the solution is not satisfactory. |
I think this issue is still valid. From my understanding, what this issue suggests is removing the below 3 lines from |
I just started building everything in Deno - even when node is the target (after a bundle and some creative imports). Absolutely love it.
I found something that seems odd to me in std / assert. I would like to make a PR for it, but first I would like to hear if there is a (good) reason it is currently implemented like this.
The
assertThrows
as described inhttps://deno.land/manual/testing/assertions
is documented as if it asserts that "something" is being thrown. Yetreturns
AssertionError: A non-Error object was thrown.
. You will have to return a properError
to get a passOn MDN the throw statement is defined as
It seems odd to demand a thrown exception is of a specific type (
Error
) when the nameassertThrows
indicates that it is testing if something is thrown.I suggest that the current behaviour gets renamed to
assertThrowsError
andassertThrows
is changed to disregard what type is thrown (also it's async sibling).Let me know if there is some hidden knowledge or good reasons for the current behaviour. And if that is the case I'm happy to make a PR to the docs repo to explain it better to the next dev coming along this path of the world of Deno.
I do understand that it is a breaking change, but better to sort out consistency early than carrying confusion into the future... right? If not, maybe a way forward would be to add
assertThrowsException
to verify that an exception was thrown but disregard the type.The text was updated successfully, but these errors were encountered: