Skip to content
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

Stacktraces prevent Err instances being equal #187

Open
grodin opened this issue Jan 27, 2025 · 1 comment
Open

Stacktraces prevent Err instances being equal #187

grodin opened this issue Jan 27, 2025 · 1 comment

Comments

@grodin
Copy link

grodin commented Jan 27, 2025

This came up whilst writing tests. I wanted to assert that an Err instance was what I expected, using Vitest's expect/toEqual. That looks like:

expect(error).toEqual(Err(something))

and recursively compares error and Err(something) property by property.

This fails even when error is essentially identical to Err(something), because the _stack property isn't equal (and can't be).

The workaround is easy, just check and unwrap the Err, then compare the inner value, but it's a slight gotcha if you're not expecting it.

I think this and #77 suggest that maybe stacktrace capturing should be optional, though I'm not really sure what an ergonomic API for that would look like.

Thanks for maintaining this, btw. A Rust-like Result for TS is really useful!

@jstasiak
Copy link
Collaborator

Yeah that's a fair point, maybe a design decision we need to revisit.

We have a simple custom jest matcher that allows for expect(error).toEqualResult(Err(something)) but it's cumbersome (you need to remember to use it and it currently doesn't support all jest magic with something being a special matcher for example).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants