Skip to content

Commit

Permalink
Fix asymmetric equal for Number (#7948)
Browse files Browse the repository at this point in the history
* Fix asymmetric equal for Number

* Update CHANGELOG

* Handle the case of a being a new Number

* Use Object.is for Number equality

* Add unit tests related to issues/7941

* Add extra units for equality

* Remove unnecessary comment in eq
  • Loading branch information
dubzzz authored and pedrottimark committed Feb 25, 2019
1 parent 51817fd commit bcaa146
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
49 changes: 49 additions & 0 deletions packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1818,6 +1818,13 @@ Expected: <green>\\"abc\\"</>
Received: <red>\\"abc\\"</>"
`;

exports[`.toEqual() {pass: false} expect("abc").not.toEqual({"0": "a", "1": "b", "2": "c"}) 1`] = `
"<dim>expect(</><red>received</><dim>).</>not<dim>.toEqual(</><green>expected</><dim>)</>

Expected: <green>{\\"0\\": \\"a\\", \\"1\\": \\"b\\", \\"2\\": \\"c\\"}</>
Received: <red>\\"abc\\"</>"
`;

exports[`.toEqual() {pass: false} expect("abcd").not.toEqual(StringContaining "bc") 1`] = `
"<dim>expect(</><red>received</><dim>).</>not<dim>.toEqual(</><green>expected</><dim>)</>

Expand Down Expand Up @@ -1931,6 +1938,13 @@ Expected: <green>Any<Function></>
Received: <red>[Function anonymous]</>"
`;

exports[`.toEqual() {pass: false} expect({"0": "a", "1": "b", "2": "c"}).not.toEqual("abc") 1`] = `
"<dim>expect(</><red>received</><dim>).</>not<dim>.toEqual(</><green>expected</><dim>)</>

Expected: <green>\\"abc\\"</>
Received: <red>{\\"0\\": \\"a\\", \\"1\\": \\"b\\", \\"2\\": \\"c\\"}</>"
`;

exports[`.toEqual() {pass: false} expect({"a": 1, "b": [Function b], "c": true}).not.toEqual({"a": 1, "b": Any<Function>, "c": Anything}) 1`] = `
"<dim>expect(</><red>received</><dim>).</>not<dim>.toEqual(</><green>expected</><dim>)</>

Expand Down Expand Up @@ -2006,13 +2020,34 @@ Expected: <green>{}</>
Received: <red>{}</>"
`;

exports[`.toEqual() {pass: false} expect({}).not.toEqual(0) 1`] = `
"<dim>expect(</><red>received</><dim>).</>not<dim>.toEqual(</><green>expected</><dim>)</>

Expected: <green>0</>
Received: <red>{}</>"
`;

exports[`.toEqual() {pass: false} expect(0).not.toEqual({}) 1`] = `
"<dim>expect(</><red>received</><dim>).</>not<dim>.toEqual(</><green>expected</><dim>)</>

Expected: <green>{}</>
Received: <red>0</>"
`;

exports[`.toEqual() {pass: false} expect(0).toEqual(-0) 1`] = `
"<dim>expect(</><red>received</><dim>).toEqual(</><green>expected</><dim>)</>

Expected: <green>-0</>
Received: <red>0</>"
`;

exports[`.toEqual() {pass: false} expect(0).toEqual(5e-324) 1`] = `
"<dim>expect(</><red>received</><dim>).toEqual(</><green>expected</><dim>)</>

Expected: <green>5e-324</>
Received: <red>0</>"
`;

exports[`.toEqual() {pass: false} expect(1).not.toEqual(1) 1`] = `
"<dim>expect(</><red>received</><dim>).</>not<dim>.toEqual(</><green>expected</><dim>)</>

Expand All @@ -2034,6 +2069,13 @@ Expected: <green>ArrayContaining [1, 2]</>
Received: <red>1</>"
`;

exports[`.toEqual() {pass: false} expect(5e-324).toEqual(0) 1`] = `
"<dim>expect(</><red>received</><dim>).toEqual(</><green>expected</><dim>)</>

Expected: <green>0</>
Received: <red>5e-324</>"
`;

exports[`.toEqual() {pass: false} expect(Immutable.List [1, 2]).not.toEqual(Immutable.List [1, 2]) 1`] = `
"<dim>expect(</><red>received</><dim>).</>not<dim>.toEqual(</><green>expected</><dim>)</>

Expand Down Expand Up @@ -2394,6 +2436,13 @@ Expected: <green>Map {2 => [\\"two\\"], 1 => [\\"one\\"]}</>
Received: <red>Map {1 => [\\"one\\"], 2 => [\\"two\\"]}</>"
`;

exports[`.toEqual() {pass: false} expect(NaN).not.toEqual(NaN) 1`] = `
"<dim>expect(</><red>received</><dim>).</>not<dim>.toEqual(</><green>expected</><dim>)</>

Expected: <green>NaN</>
Received: <red>NaN</>"
`;

exports[`.toEqual() {pass: false} expect(Set {[1], [2], [3], [3]}).not.toEqual(Set {[3], [3], [2], [1]}) 1`] = `
"<dim>expect(</><red>received</><dim>).</>not<dim>.toEqual(</><green>expected</><dim>)</>

Expand Down
11 changes: 11 additions & 0 deletions packages/expect/src/__tests__/matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -426,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])],
Expand Down
4 changes: 1 addition & 3 deletions packages/expect/src/jasmineUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ 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 a != +a ? b != +b : a === 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
Expand Down

0 comments on commit bcaa146

Please sign in to comment.