lodash / lodash

A modern JavaScript utility library delivering modularity, performance, & extras.
https://lodash.com/
Other
59.63k stars 7.02k forks source link

test failed on 0 != -0 #5740

Open tisonkun opened 12 months ago

tisonkun commented 12 months ago
    [
      0,
      0,
      0,
      0,
      0,
      0,
+     0,
+     0
-     -0,
-     -0
    ]
  ]

- Expected  - 6
+ Received  + 6
            it(`\`_.${methodName}\` should convert number primitives and objects to numbers`, () => {
                const values = [2, 1.2, MAX_SAFE_INTEGER, MAX_INTEGER, Infinity, NaN];

                const expected = lodashStable.map(values, (value) => {
                    if (!isToNumber) {
                        if (!isToFinite && value === 1.2) {
                            value = 1;
                        } else if (value === Infinity) {
                            value = MAX_INTEGER;
                        } else if (value !== value) {
                            value = 0;
                        }
                        if (isToLength || isToSafeInteger) {
                            value = Math.min(
                                value,
                                isToLength ? MAX_ARRAY_LENGTH : MAX_SAFE_INTEGER,
                            );
                        }
                    }
                    const neg = isToLength ? 0 : -value; // <-- this line
                    return [value, value, neg, neg];
                });

                const actual = lodashStable.map(values, (value) => [
                    func(value),
                    func(Object(value)),
                    func(-value), // <-- and this line
                    func(Object(-value)), // <-- and this line
                ]);

                expect(actual).toEqual(expected);
            });

I don't know whether it's a bun issue. cc @jdalton

Or it can be a jest issue.

tisonkun commented 12 months ago

Seems this issue - https://stackoverflow.com/questions/59343675/javascript-jest-expect-0-equal-0

tisonkun commented 12 months ago

... and https://github.com/jestjs/jest/issues/12221

tisonkun commented 12 months ago

Here is a monkey patch for fix:

diff --git a/test/number-coercion-methods.spec.js b/test/number-coercion-methods.spec.js
index af299e68d..81d03ee25 100644
--- a/test/number-coercion-methods.spec.js
+++ b/test/number-coercion-methods.spec.js
@@ -89,7 +89,10 @@ describe('number coercion methods', () => {
                             );
                         }
                     }
-                    const neg = isToLength ? 0 : -value;
+                    let neg = isToLength ? 0 : -value;
+                    if (neg === 0) {
+                        neg = 0;
+                    }
                     return [value, value, neg, neg];
                 });

@@ -134,7 +137,12 @@ describe('number coercion methods', () => {
                             n = Math.min(n, isToLength ? MAX_ARRAY_LENGTH : MAX_SAFE_INTEGER);
                         }
                     }
-                    const neg = isToLength ? 0 : -n;
+                    let neg = isToLength ? 0 : -n;
+                    if (Number.isNaN(neg)) {
+                        neg = Number.NaN;
+                    } else if (neg === 0) {
+                        neg = 0;
+                    }
                     return [n, n, n, n, n, n, neg, neg];
                 });
jdalton commented 11 months ago

It's a bun issue! I'll report it and link back ASAP.

Actually bun is more correct here. We want to side with bun here and respect -0 if our APIs produce it.

jdalton commented 11 months ago

In bun if I do:

import { expect, test } from 'bun:test';

test('woo', () => {
   expect([-0]).toEqual([-0]) // pass
});

but

expect([0]).toEqual([-0]) // fail

So bun seems to respect -0 :tada

tisonkun commented 11 months ago

@jdalton Sounds like we should update the test to follow the expected manner. If so, I can apply the patch above for doing so.