From 0041dd73517230c58a05156aff307193c6e0f90e Mon Sep 17 00:00:00 2001 From: Nicolas DUBIEN Date: Thu, 21 Feb 2019 21:12:34 +0100 Subject: [PATCH 1/7] Fix asymmetric equal for Number --- packages/expect/src/jasmineUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/expect/src/jasmineUtils.ts b/packages/expect/src/jasmineUtils.ts index f9945dbf64df..20323d64f5ef 100644 --- a/packages/expect/src/jasmineUtils.ts +++ b/packages/expect/src/jasmineUtils.ts @@ -108,7 +108,7 @@ function eq( case '[object Number]': // `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 a != +a ? b != +b : a === 0 && b === 0 ? 1 / a == 1 / b : a == +b; case '[object Date]': case '[object Boolean]': // Coerce dates and booleans to numeric primitive values. Dates are compared by their From 2582410c9cbcf52edfb5aed106bc0d3891e053f1 Mon Sep 17 00:00:00 2001 From: Nicolas DUBIEN Date: Thu, 21 Feb 2019 21:19:43 +0100 Subject: [PATCH 2/7] Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a098e0daece..125008ddb473 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ - `[jest-cli]` Refactor `-o` and `--coverage` combined ([#7611](https://github.com/facebook/jest/pull/7611)) - `[expect]` Fix custom async matcher stack trace ([#7652](https://github.com/facebook/jest/pull/7652)) - `[expect]` Fix `toStrictEqual` not considering arrays with objects having undefined values correctly ([#7938](https://github.com/facebook/jest/pull/7938)) +- `[expect]` Fix non-symmetric equal for Number ([#7948](https://github.com/facebook/jest/pull/7948)) - `[jest-changed-files]` Improve default file selection for Mercurial repos ([#7880](https://github.com/facebook/jest/pull/7880)) - `[jest-validate]` Fix validating async functions ([#7894](https://github.com/facebook/jest/issues/7894)) - `[jest-circus]` Fix bug with test.only ([#7888](https://github.com/facebook/jest/pull/7888)) From 997add9533f3df4365de4ec6f08a340d77fc2997 Mon Sep 17 00:00:00 2001 From: Nicolas DUBIEN Date: Thu, 21 Feb 2019 22:44:05 +0100 Subject: [PATCH 3/7] Handle the case of a being a new Number --- packages/expect/src/jasmineUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/expect/src/jasmineUtils.ts b/packages/expect/src/jasmineUtils.ts index 20323d64f5ef..ea31772ad236 100644 --- a/packages/expect/src/jasmineUtils.ts +++ b/packages/expect/src/jasmineUtils.ts @@ -108,7 +108,7 @@ function eq( case '[object Number]': // `NaN`s are equivalent, but non-reflexive. An `egal` comparison is performed for // other numeric values. - return a != +a ? b != +b : a === 0 && b === 0 ? 1 / a == 1 / b : a == +b; + return a != +a ? b != +b : Number(a) === 0 && Number(b) === 0 ? 1 / a == 1 / b : a == +b; case '[object Date]': case '[object Boolean]': // Coerce dates and booleans to numeric primitive values. Dates are compared by their From 84de9dad7dcf71775756ee20eaa08ad4b51c9372 Mon Sep 17 00:00:00 2001 From: Nicolas DUBIEN Date: Thu, 21 Feb 2019 22:52:32 +0100 Subject: [PATCH 4/7] Use Object.is for Number equality --- packages/expect/src/jasmineUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/expect/src/jasmineUtils.ts b/packages/expect/src/jasmineUtils.ts index ea31772ad236..c5069ee71407 100644 --- a/packages/expect/src/jasmineUtils.ts +++ b/packages/expect/src/jasmineUtils.ts @@ -108,7 +108,7 @@ function eq( case '[object Number]': // `NaN`s are equivalent, but non-reflexive. An `egal` comparison is performed for // other numeric values. - return a != +a ? b != +b : Number(a) === 0 && Number(b) === 0 ? 1 / a == 1 / b : a == +b; + return Object.is(Number(a), Number(b)); case '[object Date]': case '[object Boolean]': // Coerce dates and booleans to numeric primitive values. Dates are compared by their From f871c98c4ce53f3b5043697c43d4b8546754e91c Mon Sep 17 00:00:00 2001 From: Nicolas DUBIEN Date: Fri, 22 Feb 2019 00:06:22 +0100 Subject: [PATCH 5/7] Add unit tests related to issues/7941 --- .../__tests__/__snapshots__/matchers.test.js.snap | 14 ++++++++++++++ packages/expect/src/__tests__/matchers.test.js | 2 ++ 2 files changed, 16 insertions(+) diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index 8e07feaf3198..59e4939dc774 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -2013,6 +2013,13 @@ Expected: -0 Received: 0" `; +exports[`.toEqual() {pass: false} expect(0).toEqual(5e-324) 1`] = ` +"expect(received).toEqual(expected) + +Expected: 5e-324 +Received: 0" +`; + exports[`.toEqual() {pass: false} expect(1).not.toEqual(1) 1`] = ` "expect(received).not.toEqual(expected) @@ -2034,6 +2041,13 @@ Expected: ArrayContaining [1, 2] Received: 1" `; +exports[`.toEqual() {pass: false} expect(5e-324).toEqual(0) 1`] = ` +"expect(received).toEqual(expected) + +Expected: 0 +Received: 5e-324" +`; + exports[`.toEqual() {pass: false} expect(Immutable.List [1, 2]).not.toEqual(Immutable.List [1, 2]) 1`] = ` "expect(received).not.toEqual(expected) diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index 89435ff47c1d..dcbedb655572 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -344,6 +344,8 @@ describe('.toEqual()', () => { [true, false], [1, 2], [0, -0], + [0, Number.MIN_VALUE], // issues/7941 + [Number.MIN_VALUE, 0], [{a: 5}, {b: 6}], ['banana', 'apple'], [null, undefined], From 94e82b8c23d17083951fda517ccdb723aa338eba Mon Sep 17 00:00:00 2001 From: Nicolas DUBIEN Date: Fri, 22 Feb 2019 00:11:47 +0100 Subject: [PATCH 6/7] Add extra units for equality --- .../__snapshots__/matchers.test.js.snap | 35 +++++++++++++++++++ .../expect/src/__tests__/matchers.test.js | 9 +++++ 2 files changed, 44 insertions(+) diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index 59e4939dc774..d576cdbad479 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -1818,6 +1818,13 @@ Expected: \\"abc\\" Received: \\"abc\\"" `; +exports[`.toEqual() {pass: false} expect("abc").not.toEqual({"0": "a", "1": "b", "2": "c"}) 1`] = ` +"expect(received).not.toEqual(expected) + +Expected: {\\"0\\": \\"a\\", \\"1\\": \\"b\\", \\"2\\": \\"c\\"} +Received: \\"abc\\"" +`; + exports[`.toEqual() {pass: false} expect("abcd").not.toEqual(StringContaining "bc") 1`] = ` "expect(received).not.toEqual(expected) @@ -1931,6 +1938,13 @@ Expected: Any Received: [Function anonymous]" `; +exports[`.toEqual() {pass: false} expect({"0": "a", "1": "b", "2": "c"}).not.toEqual("abc") 1`] = ` +"expect(received).not.toEqual(expected) + +Expected: \\"abc\\" +Received: {\\"0\\": \\"a\\", \\"1\\": \\"b\\", \\"2\\": \\"c\\"}" +`; + exports[`.toEqual() {pass: false} expect({"a": 1, "b": [Function b], "c": true}).not.toEqual({"a": 1, "b": Any, "c": Anything}) 1`] = ` "expect(received).not.toEqual(expected) @@ -2006,6 +2020,20 @@ Expected: {} Received: {}" `; +exports[`.toEqual() {pass: false} expect({}).not.toEqual(0) 1`] = ` +"expect(received).not.toEqual(expected) + +Expected: 0 +Received: {}" +`; + +exports[`.toEqual() {pass: false} expect(0).not.toEqual({}) 1`] = ` +"expect(received).not.toEqual(expected) + +Expected: {} +Received: 0" +`; + exports[`.toEqual() {pass: false} expect(0).toEqual(-0) 1`] = ` "expect(received).toEqual(expected) @@ -2408,6 +2436,13 @@ Expected: Map {2 => [\\"two\\"], 1 => [\\"one\\"]} Received: Map {1 => [\\"one\\"], 2 => [\\"two\\"]}" `; +exports[`.toEqual() {pass: false} expect(NaN).not.toEqual(NaN) 1`] = ` +"expect(received).not.toEqual(expected) + +Expected: NaN +Received: NaN" +`; + exports[`.toEqual() {pass: false} expect(Set {[1], [2], [3], [3]}).not.toEqual(Set {[3], [3], [2], [1]}) 1`] = ` "expect(received).not.toEqual(expected) diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index dcbedb655572..4e2cf535b6b3 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -428,7 +428,16 @@ describe('.toEqual()', () => { [ [true, true], [1, 1], + [NaN, NaN], + // eslint-disable-next-line no-new-wrappers + [0, new Number(0)], + // eslint-disable-next-line no-new-wrappers + [new Number(0), 0], ['abc', 'abc'], + // eslint-disable-next-line no-new-wrappers + [new String('abc'), 'abc'], + // eslint-disable-next-line no-new-wrappers + ['abc', new String('abc')], [[1], [1]], [[1, 2], [1, 2]], [Immutable.List([1]), Immutable.List([1])], From e9a187389ece6d074a9c1a8d7f55b09a75c65a15 Mon Sep 17 00:00:00 2001 From: Nicolas DUBIEN Date: Fri, 22 Feb 2019 01:08:00 +0100 Subject: [PATCH 7/7] Remove unnecessary comment in eq --- packages/expect/src/jasmineUtils.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/expect/src/jasmineUtils.ts b/packages/expect/src/jasmineUtils.ts index c5069ee71407..11e368d11001 100644 --- a/packages/expect/src/jasmineUtils.ts +++ b/packages/expect/src/jasmineUtils.ts @@ -106,8 +106,6 @@ function eq( // $FlowFixMe – Flow sees `a` as a number return a == String(b); case '[object Number]': - // `NaN`s are equivalent, but non-reflexive. An `egal` comparison is performed for - // other numeric values. return Object.is(Number(a), Number(b)); case '[object Date]': case '[object Boolean]':