From faa7c3b9f935b63874932d7b606e8976c555c440 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Conde?= Date: Fri, 30 Nov 2018 19:46:09 +0000 Subject: [PATCH 01/10] toBeCloseTo works properly when comparing with Infinity --- .../__snapshots__/matchers.test.js.snap | 40 +++++++++++++++++++ .../expect/src/__tests__/matchers.test.js | 22 ++++++++++ packages/expect/src/matchers.js | 10 ++++- 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index 6e9c035453aa..ad52eb43b2e1 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -368,6 +368,38 @@ Expected: false Received: true" `; +exports[`.toBeCloseTo() {pass: false} expect(-Infinity)toBeCloseTo( -1.23) 1`] = ` +"expect(received).toBeCloseTo(expected) + +Precision: 2-digit +Expected: -1.23 +Received: -Infinity" +`; + +exports[`.toBeCloseTo() {pass: false} expect(Infinity)toBeCloseTo( -Infinity) 1`] = ` +"expect(received).toBeCloseTo(expected) + +Precision: 2-digit +Expected: -Infinity +Received: Infinity" +`; + +exports[`.toBeCloseTo() {pass: false} expect(Infinity)toBeCloseTo( 1.23) 1`] = ` +"expect(received).toBeCloseTo(expected) + +Precision: 2-digit +Expected: 1.23 +Received: Infinity" +`; + +exports[`.toBeCloseTo() {pass: true} expect(-Infinity)toBeCloseTo( -Infinity) 1`] = ` +"expect(received).not.toBeCloseTo(expected) + +Precision: 2-digit +Expected: -Infinity +Received: -Infinity" +`; + exports[`.toBeCloseTo() {pass: true} expect(0)toBeCloseTo( 0) 1`] = ` "expect(received).not.toBeCloseTo(expected) @@ -416,6 +448,14 @@ Expected: 1.234 Received: 1.23" `; +exports[`.toBeCloseTo() {pass: true} expect(Infinity)toBeCloseTo( Infinity) 1`] = ` +"expect(received).not.toBeCloseTo(expected) + +Precision: 2-digit +Expected: Infinity +Received: Infinity" +`; + exports[`.toBeCloseTo() accepts an optional precision argument: [0, 0.000004, 5] 1`] = ` "expect(received).not.toBeCloseTo(expected, precision) diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index 83662ae03c41..6fa7323a1198 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -894,6 +894,28 @@ describe('.toBeCloseTo()', () => { }); }); + [[Infinity, Infinity], [-Infinity, -Infinity]].forEach(([n1, n2]) => { + it(`{pass: true} expect(${n1})toBeCloseTo( ${n2})`, () => { + jestExpect(n1).toBeCloseTo(n2); + + expect(() => + jestExpect(n1).not.toBeCloseTo(n2), + ).toThrowErrorMatchingSnapshot(); + }); + }); + + [[Infinity, -Infinity], [Infinity, 1.23], [-Infinity, -1.23]].forEach( + ([n1, n2]) => { + it(`{pass: false} expect(${n1})toBeCloseTo( ${n2})`, () => { + jestExpect(n1).not.toBeCloseTo(n2); + + expect(() => + jestExpect(n1).toBeCloseTo(n2), + ).toThrowErrorMatchingSnapshot(); + }); + }, + ); + [[0, 0.1, 0], [0, 0.0001, 3], [0, 0.000004, 5]].forEach(([n1, n2, p]) => { it(`accepts an optional precision argument: [${n1}, ${n2}, ${p}]`, () => { jestExpect(n1).toBeCloseTo(n2, p); diff --git a/packages/expect/src/matchers.js b/packages/expect/src/matchers.js index 13e516e48267..20cc4d1034f9 100644 --- a/packages/expect/src/matchers.js +++ b/packages/expect/src/matchers.js @@ -87,7 +87,15 @@ const matchers: MatchersObject = { toBeCloseTo(actual: number, expected: number, precision?: number = 2) { const secondArgument = arguments.length === 3 ? 'precision' : null; ensureNumbers(actual, expected, '.toBeCloseTo'); - const pass = Math.abs(expected - actual) < Math.pow(10, -precision) / 2; + + let pass = false; + //If both actual and expected are Infinity it is true they are close + if (actual == Infinity && expected == Infinity) pass = true; + //If both actual and expected are -Infinity it is true they are close + else if (actual == -Infinity && expected == -Infinity) pass = true; + //actual and expected are close to each other taking precision into account + else pass = Math.abs(expected - actual) < Math.pow(10, -precision) / 2; + const message = () => matcherHint('.toBeCloseTo', undefined, undefined, { isNot: this.isNot, From 30e1da00a911b27868a6001d8c60dc5209fc1a06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Conde?= Date: Fri, 30 Nov 2018 19:51:36 +0000 Subject: [PATCH 02/10] CHANGELOG update --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64b83d253735..cd9afbf23613 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ ## master ### Features - +- `[expect]` expect(Infinity).toBeCloseTo(Infinity) works as intended aswell as with -Infinity ([#7405](https://github.com/facebook/jest/pull/7405)) - `[jest-cli]` [**BREAKING**] Only set error process error codes when they are non-zero ([#7363](https://github.com/facebook/jest/pull/7363)) - `[jest-config]` [**BREAKING**] Deprecate `setupTestFrameworkScriptFile` in favor of new `setupFilesAfterEnv` ([#7119](https://github.com/facebook/jest/pull/7119)) - `[jest-worker]` [**BREAKING**] Add functionality to call a `setup` method in the worker before the first call and a `teardown` method when ending the farm ([#7014](https://github.com/facebook/jest/pull/7014)) From b39bf256aa6e5d4dff792b3eef9c64604c743eb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Conde?= Date: Fri, 30 Nov 2018 21:12:42 +0000 Subject: [PATCH 03/10] Linter issue solved --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cd9afbf23613..696b40ad6a5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## master ### Features + - `[expect]` expect(Infinity).toBeCloseTo(Infinity) works as intended aswell as with -Infinity ([#7405](https://github.com/facebook/jest/pull/7405)) - `[jest-cli]` [**BREAKING**] Only set error process error codes when they are non-zero ([#7363](https://github.com/facebook/jest/pull/7363)) - `[jest-config]` [**BREAKING**] Deprecate `setupTestFrameworkScriptFile` in favor of new `setupFilesAfterEnv` ([#7119](https://github.com/facebook/jest/pull/7119)) From c03e22f49efbcc1f0644ad062721213340335e82 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 1 Dec 2018 21:28:18 +0000 Subject: [PATCH 04/10] Apply suggestions from code review Co-Authored-By: joao-conde --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 696b40ad6a5f..e1ce0fad2ae6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ### Features -- `[expect]` expect(Infinity).toBeCloseTo(Infinity) works as intended aswell as with -Infinity ([#7405](https://github.com/facebook/jest/pull/7405)) +- `[expect]` `expect(Infinity).toBeCloseTo(Infinity)` works as intended aswell as with `_Infinity` ([#7405](https://github.com/facebook/jest/pull/7405)) - `[jest-cli]` [**BREAKING**] Only set error process error codes when they are non-zero ([#7363](https://github.com/facebook/jest/pull/7363)) - `[jest-config]` [**BREAKING**] Deprecate `setupTestFrameworkScriptFile` in favor of new `setupFilesAfterEnv` ([#7119](https://github.com/facebook/jest/pull/7119)) - `[jest-worker]` [**BREAKING**] Add functionality to call a `setup` method in the worker before the first call and a `teardown` method when ending the farm ([#7014](https://github.com/facebook/jest/pull/7014)) From 96c87311b625182e261c1f7599e1a19b30606b97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Dias=20Conde=20Azevedo?= Date: Sat, 1 Dec 2018 21:32:45 +0000 Subject: [PATCH 05/10] Update matchers.js --- packages/expect/src/matchers.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/expect/src/matchers.js b/packages/expect/src/matchers.js index 20cc4d1034f9..3adc39112717 100644 --- a/packages/expect/src/matchers.js +++ b/packages/expect/src/matchers.js @@ -88,12 +88,13 @@ const matchers: MatchersObject = { const secondArgument = arguments.length === 3 ? 'precision' : null; ensureNumbers(actual, expected, '.toBeCloseTo'); - let pass = false; - //If both actual and expected are Infinity it is true they are close + let pass = false;Check that both are not equal to the Infinity global + //If both actual and expected are equal to the Infinity global if (actual == Infinity && expected == Infinity) pass = true; - //If both actual and expected are -Infinity it is true they are close + //If both actual and expected are equal to the symmetric of the Infinity global else if (actual == -Infinity && expected == -Infinity) pass = true; - //actual and expected are close to each other taking precision into account + //both actual and expected are not equal to the Infinity global + //evaluate if they are close to each other taking precision into account else pass = Math.abs(expected - actual) < Math.pow(10, -precision) / 2; const message = () => From c3ac037ea16976f10c6fee40c75b371f6c57aebf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Conde?= Date: Sat, 1 Dec 2018 21:36:45 +0000 Subject: [PATCH 06/10] Update CHANGELOG --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1ce0fad2ae6..7714caaf929a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,6 @@ ### Features -- `[expect]` `expect(Infinity).toBeCloseTo(Infinity)` works as intended aswell as with `_Infinity` ([#7405](https://github.com/facebook/jest/pull/7405)) - `[jest-cli]` [**BREAKING**] Only set error process error codes when they are non-zero ([#7363](https://github.com/facebook/jest/pull/7363)) - `[jest-config]` [**BREAKING**] Deprecate `setupTestFrameworkScriptFile` in favor of new `setupFilesAfterEnv` ([#7119](https://github.com/facebook/jest/pull/7119)) - `[jest-worker]` [**BREAKING**] Add functionality to call a `setup` method in the worker before the first call and a `teardown` method when ending the farm ([#7014](https://github.com/facebook/jest/pull/7014)) @@ -33,6 +32,7 @@ - `[jest-config]` Add `dependencyExtractor` option to use a custom module to extract dependencies from files ([#7313](https://github.com/facebook/jest/pull/7313), [#7349](https://github.com/facebook/jest/pull/7349), [#7350](https://github.com/facebook/jest/pull/7350)) - `[jest-haste-map]` Accept a `getCacheKey` method in `hasteImplModulePath` modules to reset the cache when the logic changes ([#7350](https://github.com/facebook/jest/pull/7350)) - `[jest-config]` Add `haste.computeSha1` option to compute the sha-1 of the files in the haste map ([#7345](https://github.com/facebook/jest/pull/7345)) +- `[expect]` `expect(Infinity).toBeCloseTo(Infinity)` works as intended aswell as with `_Infinity` ([#7405](https://github.com/facebook/jest/pull/7405)) ### Fixes From 425d175bfe234bb242e5b41ab84c7d6612d9fd77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Conde?= Date: Sat, 1 Dec 2018 21:39:07 +0000 Subject: [PATCH 07/10] Fixed bug, leftover of old comment --- packages/expect/src/matchers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/expect/src/matchers.js b/packages/expect/src/matchers.js index 3adc39112717..38013da1a807 100644 --- a/packages/expect/src/matchers.js +++ b/packages/expect/src/matchers.js @@ -88,7 +88,7 @@ const matchers: MatchersObject = { const secondArgument = arguments.length === 3 ? 'precision' : null; ensureNumbers(actual, expected, '.toBeCloseTo'); - let pass = false;Check that both are not equal to the Infinity global + let pass = false; //If both actual and expected are equal to the Infinity global if (actual == Infinity && expected == Infinity) pass = true; //If both actual and expected are equal to the symmetric of the Infinity global From 56cac6242e2934748321fa97618a2420d1fb9d98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Conde?= Date: Sat, 1 Dec 2018 22:05:03 +0000 Subject: [PATCH 08/10] Removed unecessary self-explanatory comments --- packages/expect/src/matchers.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/expect/src/matchers.js b/packages/expect/src/matchers.js index 38013da1a807..a7f987a9a85c 100644 --- a/packages/expect/src/matchers.js +++ b/packages/expect/src/matchers.js @@ -89,12 +89,9 @@ const matchers: MatchersObject = { ensureNumbers(actual, expected, '.toBeCloseTo'); let pass = false; - //If both actual and expected are equal to the Infinity global + if (actual == Infinity && expected == Infinity) pass = true; - //If both actual and expected are equal to the symmetric of the Infinity global else if (actual == -Infinity && expected == -Infinity) pass = true; - //both actual and expected are not equal to the Infinity global - //evaluate if they are close to each other taking precision into account else pass = Math.abs(expected - actual) < Math.pow(10, -precision) / 2; const message = () => From 254ed09fd3ea37276fad4378048bfa1693782f6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Conde?= Date: Sat, 1 Dec 2018 22:07:49 +0000 Subject: [PATCH 09/10] Updated CHANGELOG --- CHANGELOG.md | 2 +- .../__tests__/handle-property-matchers.test.js | 10 ++++++++++ .../handle-property-matchers-with-name.test.js.snap | 10 ++++++++++ .../handle-property-matchers-with-name.test.js | 5 +++++ 4 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 e2e/toMatchInlineSnapshot/__tests__/handle-property-matchers.test.js create mode 100644 e2e/toMatchSnapshot/__tests__/__snapshots__/handle-property-matchers-with-name.test.js.snap create mode 100644 e2e/toMatchSnapshot/__tests__/handle-property-matchers-with-name.test.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 7714caaf929a..622c61c3e303 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,7 +32,7 @@ - `[jest-config]` Add `dependencyExtractor` option to use a custom module to extract dependencies from files ([#7313](https://github.com/facebook/jest/pull/7313), [#7349](https://github.com/facebook/jest/pull/7349), [#7350](https://github.com/facebook/jest/pull/7350)) - `[jest-haste-map]` Accept a `getCacheKey` method in `hasteImplModulePath` modules to reset the cache when the logic changes ([#7350](https://github.com/facebook/jest/pull/7350)) - `[jest-config]` Add `haste.computeSha1` option to compute the sha-1 of the files in the haste map ([#7345](https://github.com/facebook/jest/pull/7345)) -- `[expect]` `expect(Infinity).toBeCloseTo(Infinity)` works as intended aswell as with `_Infinity` ([#7405](https://github.com/facebook/jest/pull/7405)) +- `[expect]` `expect(Infinity).toBeCloseTo(Infinity)` Treats `Infinity` as equal in toBeCloseTo matcher ([#7405](https://github.com/facebook/jest/pull/7405)) ### Fixes diff --git a/e2e/toMatchInlineSnapshot/__tests__/handle-property-matchers.test.js b/e2e/toMatchInlineSnapshot/__tests__/handle-property-matchers.test.js new file mode 100644 index 000000000000..93f7f948dd31 --- /dev/null +++ b/e2e/toMatchInlineSnapshot/__tests__/handle-property-matchers.test.js @@ -0,0 +1,10 @@ +test('handles property matchers', () => { + expect({createdAt: 'string'}).toMatchInlineSnapshot( + {createdAt: expect.any(String)}, + ` +Object { + "createdAt": Any, +} +` + ); +}); diff --git a/e2e/toMatchSnapshot/__tests__/__snapshots__/handle-property-matchers-with-name.test.js.snap b/e2e/toMatchSnapshot/__tests__/__snapshots__/handle-property-matchers-with-name.test.js.snap new file mode 100644 index 000000000000..78b8823efa4e --- /dev/null +++ b/e2e/toMatchSnapshot/__tests__/__snapshots__/handle-property-matchers-with-name.test.js.snap @@ -0,0 +1,10 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`handles property matchers with deep properties 1`] = ` +Object { + "user": Object { + "createdAt": Any, + "name": "Jest", + }, +} +`; diff --git a/e2e/toMatchSnapshot/__tests__/handle-property-matchers-with-name.test.js b/e2e/toMatchSnapshot/__tests__/handle-property-matchers-with-name.test.js new file mode 100644 index 000000000000..c04d3646b05a --- /dev/null +++ b/e2e/toMatchSnapshot/__tests__/handle-property-matchers-with-name.test.js @@ -0,0 +1,5 @@ +test('handles property matchers with deep properties', () => { + expect({user: {createdAt: new Date(), name: 'CHANGED'}}).toMatchSnapshot({ + user: {createdAt: expect.any(Date), name: 'CHANGED'}, + }); +}); From ac4511ca563952ab78c0bdcc5c12100997cf16df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Conde?= Date: Sat, 1 Dec 2018 22:23:15 +0000 Subject: [PATCH 10/10] Snapshot updates --- .../__tests__/handle-property-matchers.test.js | 10 ---------- .../handle-property-matchers-with-name.test.js.snap | 10 ---------- .../handle-property-matchers-with-name.test.js | 5 ----- 3 files changed, 25 deletions(-) delete mode 100644 e2e/toMatchInlineSnapshot/__tests__/handle-property-matchers.test.js delete mode 100644 e2e/toMatchSnapshot/__tests__/__snapshots__/handle-property-matchers-with-name.test.js.snap delete mode 100644 e2e/toMatchSnapshot/__tests__/handle-property-matchers-with-name.test.js diff --git a/e2e/toMatchInlineSnapshot/__tests__/handle-property-matchers.test.js b/e2e/toMatchInlineSnapshot/__tests__/handle-property-matchers.test.js deleted file mode 100644 index 93f7f948dd31..000000000000 --- a/e2e/toMatchInlineSnapshot/__tests__/handle-property-matchers.test.js +++ /dev/null @@ -1,10 +0,0 @@ -test('handles property matchers', () => { - expect({createdAt: 'string'}).toMatchInlineSnapshot( - {createdAt: expect.any(String)}, - ` -Object { - "createdAt": Any, -} -` - ); -}); diff --git a/e2e/toMatchSnapshot/__tests__/__snapshots__/handle-property-matchers-with-name.test.js.snap b/e2e/toMatchSnapshot/__tests__/__snapshots__/handle-property-matchers-with-name.test.js.snap deleted file mode 100644 index 78b8823efa4e..000000000000 --- a/e2e/toMatchSnapshot/__tests__/__snapshots__/handle-property-matchers-with-name.test.js.snap +++ /dev/null @@ -1,10 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`handles property matchers with deep properties 1`] = ` -Object { - "user": Object { - "createdAt": Any, - "name": "Jest", - }, -} -`; diff --git a/e2e/toMatchSnapshot/__tests__/handle-property-matchers-with-name.test.js b/e2e/toMatchSnapshot/__tests__/handle-property-matchers-with-name.test.js deleted file mode 100644 index c04d3646b05a..000000000000 --- a/e2e/toMatchSnapshot/__tests__/handle-property-matchers-with-name.test.js +++ /dev/null @@ -1,5 +0,0 @@ -test('handles property matchers with deep properties', () => { - expect({user: {createdAt: new Date(), name: 'CHANGED'}}).toMatchSnapshot({ - user: {createdAt: expect.any(Date), name: 'CHANGED'}, - }); -});