-
Notifications
You must be signed in to change notification settings - Fork 88
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
Make unwrap exception test friendly #215
Make unwrap exception test friendly #215
Conversation
Hey @bluenote10, my thoughts are that keeping the stack trace is useful, especially if someone knowingly uses either Secondly, there's a trick that one can use in throw new Error('Called `_unsafeUnwrapErr` on an Ok containing:\n' + JSON.stringify(this.value, null, 2)) Which leads to the following example output in Jest:
So I think this is the path I'm going to take. |
See here: |
To my knowledge it is a bit of an anti-pattern to do |
Going to close this issue seeing that there's a implementation in |
You have a good point, I suppose that this data could be logged out in the error message automatically in test environments only. |
I might still be possible to attach a stack trace when throwing the object via something like |
I was actually playing around with this approach originally, but figured having a native |
Yes but the memory overhead is still there. If you call |
I'm going to give it some thought and then decide what changes to make before releasing this change (probably tomorrow). I was thinking maybe something like this: _unsafeUnwrapErr(): E {
const additionalInfo = isTestEnvironment()
? ' containing:\n' + JSON.stringify(this.value, null, 2)
: ''
throw new Error('Called `_unsafeUnwrapErr` on an Ok' + additionalInfo)
} However, this ends up being bad design in my opinion since the behaviour changes based on the environment that it runs in. Thus, I think it does make sense to not have a |
This would be my shot at the other action item regarding #211.
At first I tried to use a custom
UnwrapError
which inherits fromError
, and attach the extra value/error there. The problem is that Jest seems to only only ever show themessage
of an exception as soon as theinstanceof Error
check is true. Not sure if there is a way around it. With this behavior of Jest the only option would have been to attachthis.value
/this.error
to the message itself, but that sucks because, because it would most likely require someJSON.stringify
, and it prevents the final printing from pretty printing the object properly.Then I figured: The
_unsafeUnwrap
technically doesn't really need to throw an error that is instance ofError
. Perhaps it even shouldn't. It is not something this is supposed to be caught in normal code anyway, so the solution can focus on the test case. It turns out that simply throwing a plain object gives very nice test output. Examples:What do you think?