-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Vitest considers -0
and +0
to be different values
#7667
Comments
Digging into
I think |
I'm not sure if it's obvious to say which is right 🤔 > assert.equal(+0, -0)
undefined
> assert.strictEqual(+0, -0)
Uncaught AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
+ actual - expected
+ 0
- -0 Quick workaround would be to use custom equality tester like this https://stackblitz.com/edit/vitest-dev-vitest-firxqjfs?file=test%2Fbasic.test.ts expect.addEqualityTesters([
function (a, b) {
if (typeof a === 'number' && typeof b === 'number') {
return a === b;
}
},
]); |
Thanks! Adding a custom equality tester is a good solution for my use case. I noticed a code comment saying the I'm happy to close this issue if y'all prefer to leave the logic as-is. |
Describe the bug
I have a utility that may return
-0
.This is technically a distinct value from
+0
, but per IEEE_754, "negative zero" and "positive zero" should be considered to be equal in numerical comparisons.But
vitest
's.toBe()
and.toEqual()
methods appears to not consider them to be equal, causing unit tests to fail.Reproduction
https://stackblitz.com/edit/vitest-dev-vitest-dgy7dtwc?file=test%2Fbasic.test.ts
System Info
Used Package Manager
pnpm
Validations
The text was updated successfully, but these errors were encountered: