Fix asymmetric equal for Number#7948
Conversation
|
I have not added tests as I did not know where to add them. Today the |
|
I would be interested into having your opinion on #7938 (comment). It suggests to add automatic test suites to detect such bugs more easily in the future. |
|
If a recent commit broke CI, then you might need to merge the fix from master branch tomorrow. To answer your question about tests, I just added two cases after the first 3 in the describe('.toEqual()', () => {
[
[true, false],
[1, 2],
[0, -0],
[0, Number.MIN_VALUE], // issues/7941
[Number.MIN_VALUE, 0],
// and so onLook down in the test file to see that the tuples consist of For me, the first new case throws because of the error and the second writes a new snapshot. On your branch, you can expect two new snapshots in |
|
Considering the potential untested regression that you saved us (the we who is me :) from, here are 3 suggested test cases following the first 2 existing cases farther down: [
[true, true],
[1, 1],
[NaN, NaN],
[0, new Number(0)],
[new Number(0), 0],
// and so onThe add 3 new snapshots from the expected-to-fail |
|
Off topic of this pull request, serialization in this report confused me, even if I should have known: Expected: <green>0</>
Received: <red>{}</>Good thing that this edge case might only even happen in Jest test suite: expect(new Number(0)).toEqual(0); |
pedrottimark
left a comment
There was a problem hiding this comment.
Thank you. Sometimes how you don’t fix it (and why not) is as important as how you do fix it :)
|
I didn't know you could call |
|
Looking at the MDN, this seems to be a case that https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number#Syntax Probably off topic |
|
YDKJS :) Nicolas reminded me
Yes, this is an off-the-edge case in which deep equality differs from referential equality: describe('toBe', () => {
test('2 instances', () => {
expect(new Number(0)).not.toBe(new Number(0));
});
test('instance and primitive', () => {
expect(new Number(0)).not.toBe(0);
});
});EDIT: so that is reason for |
|
I do not reproduce the I'll rebase the PR to keep on track with master |
Codecov Report
@@ Coverage Diff @@
## master #7948 +/- ##
========================================
Coverage ? 64.8%
========================================
Files ? 256
Lines ? 10047
Branches ? 1464
========================================
Hits ? 6511
Misses ? 3218
Partials ? 318
Continue to review full report at Codecov.
|
|
Is there something blocking the merge of this PR? |
|
@pedrottimark feel free to merge these PRs to |
| // `NaN`s are equivalent, but non-reflexive. An `egal` comparison is performed for | ||
| // other numeric values. | ||
| return a != +a ? b != +b : a === 0 ? 1 / a == 1 / b : a == +b; | ||
| return Object.is(Number(a), Number(b)); |
No danger of not doing that 😛 |
|
@pedrottimark No worry ;) Thanks a lot for the quick reviews. You gave me really useful hints concerning where the bug comes from so that I was able to fix it |
|
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Summary
Fixes #7941:
toStrictEqualfails to distinguish 0 from 5e-324The real problem is that the
equaloperation defined intojasmineUtilsis not symmetric.Test plan
Before that change, the following assertion was failing:
Now it passes as expected.