Skip to content
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

Fix asymmetric equal for Number #7948

Merged
merged 7 commits into from
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

such a beautiful fix 😀

case '[object Date]':
case '[object Boolean]':
// Coerce dates and booleans to numeric primitive values. Dates are compared by their
Expand Down