From d873372ea9d6770cd84d8d6ef337660e869dafe3 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Sun, 28 Apr 2019 16:51:21 -0400 Subject: [PATCH 1/6] expect: Display expectedDiff more carefully --- .../__snapshots__/matchers.test.js.snap | 33 +++++++++++++++++++ .../expect/src/__tests__/matchers.test.js | 20 ++++++++--- packages/expect/src/matchers.ts | 12 +++---- packages/expect/src/print.ts | 26 +++++++++++++++ 4 files changed, 80 insertions(+), 11 deletions(-) diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index aa14992ae949..84fbe44944a7 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -463,6 +463,28 @@ Expected difference: < 0.005 Received difference: Infinity" `; +exports[`.toBeCloseTo() {pass: false} expect(3.141592e-7)toBeCloseTo( 3e-7, 8) 1`] = ` +"expect(received).toBeCloseTo(expected, precision) + +Expected: 3e-7 +Received: 3.141592e-7 + +Expected precision: 8 +Expected difference: < 5e-9 +Received difference: 1.4159200000000025e-8" +`; + +exports[`.toBeCloseTo() {pass: false} expect(56789)toBeCloseTo( 51234, -4) 1`] = ` +"expect(received).toBeCloseTo(expected, precision) + +Expected: 51234 +Received: 56789 + +Expected precision: -4 +Expected difference: < 5000 +Received difference: 5555" +`; + exports[`.toBeCloseTo() {pass: false} expect(Infinity)toBeCloseTo( -Infinity) 1`] = ` "expect(received).toBeCloseTo(expected) @@ -594,6 +616,17 @@ Expected difference: not < 0.5 Received difference: 0.1" `; +exports[`.toBeCloseTo() accepts an optional precision argument: [2.0000002, 2, 5] 1`] = ` +"expect(received).not.toBeCloseTo(expected, precision) + +Expected: not 2 +Received: 2.0000002 + +Expected precision: 5 +Expected difference: not < 5e-6 +Received difference: 2.0000000011677344e-7" +`; + exports[`.toBeCloseTo() throws: [0, 0.01] 1`] = ` "expect(received).toBeCloseTo(expected) diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index a5fbe8c37afb..70d55df2f534 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -1108,16 +1108,28 @@ describe('.toBeCloseTo()', () => { }, ); - [[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); + [[3.141592e-7, 3e-7, 8], [56789, 51234, -4]].forEach(([n1, n2, p]) => { + it(`{pass: false} expect(${n1})toBeCloseTo( ${n2}, ${p})`, () => { + jestExpect(n1).not.toBeCloseTo(n2, p); expect(() => - jestExpect(n1).not.toBeCloseTo(n2, p), + jestExpect(n1).toBeCloseTo(n2, p), ).toThrowErrorMatchingSnapshot(); }); }); + [[0, 0.1, 0], [0, 0.0001, 3], [0, 0.000004, 5], [2.0000002, 2, 5]].forEach( + ([n1, n2, p]) => { + it(`accepts an optional precision argument: [${n1}, ${n2}, ${p}]`, () => { + jestExpect(n1).toBeCloseTo(n2, p); + + expect(() => + jestExpect(n1).not.toBeCloseTo(n2, p), + ).toThrowErrorMatchingSnapshot(); + }); + }, + ); + describe('throws: Matcher error', () => { test('promise empty isNot false received', () => { const precision = 3; diff --git a/packages/expect/src/matchers.ts b/packages/expect/src/matchers.ts index 977cb205cbb3..fc31887ad4cf 100644 --- a/packages/expect/src/matchers.ts +++ b/packages/expect/src/matchers.ts @@ -26,6 +26,7 @@ import { } from 'jest-matcher-utils'; import {MatchersObject, MatcherState} from './types'; import { + printCloseTo, printDiffOrStringify, printExpectedConstructorName, printExpectedConstructorNameNot, @@ -126,8 +127,9 @@ const matchers: MatchersObject = { ) { const matcherName = 'toBeCloseTo'; const secondArgument = arguments.length === 3 ? 'precision' : undefined; + const isNot = this.isNot; const options: MatcherHintOptions = { - isNot: this.isNot, + isNot, promise: this.promise, secondArgument, }; @@ -156,18 +158,14 @@ const matchers: MatchersObject = { ? '' : `Received: ${printReceived(received)}\n` + '\n' + - `Expected precision: ${printExpected(precision)}\n` + - `Expected difference: not < ${printExpected(expectedDiff)}\n` + - `Received difference: ${printReceived(receivedDiff)}`) + printCloseTo(receivedDiff, expectedDiff, precision, isNot)) : () => matcherHint(matcherName, undefined, undefined, options) + '\n\n' + `Expected: ${printExpected(expected)}\n` + `Received: ${printReceived(received)}\n` + '\n' + - `Expected precision: ${printExpected(precision)}\n` + - `Expected difference: < ${printExpected(expectedDiff)}\n` + - `Received difference: ${printReceived(receivedDiff)}`; + printCloseTo(receivedDiff, expectedDiff, precision, isNot); return {message, pass}; }, diff --git a/packages/expect/src/print.ts b/packages/expect/src/print.ts index 1d84c9265a55..8cac1a44e823 100644 --- a/packages/expect/src/print.ts +++ b/packages/expect/src/print.ts @@ -66,6 +66,32 @@ export const printReceivedArrayContainExpectedItem = ( ']', ); +export const printCloseTo = ( + receivedDiff: number, + expectedDiff: number, + precision: number, + isNot: boolean, +): string => { + const receivedDiffString = stringify(receivedDiff); + const expectedDiffString = receivedDiffString.includes('e') + ? expectedDiff.toExponential(0) + : 0 <= precision && precision <= 20 + ? expectedDiff.toFixed(precision + 1) + : stringify(expectedDiff); + + return ( + `Expected precision: ${isNot ? ' ' : ''} ${printExpected( + precision, + )}\n` + + `Expected difference: ${isNot ? 'not ' : ''}< ${EXPECTED_COLOR( + expectedDiffString, + )}\n` + + `Received difference: ${isNot ? ' ' : ''} ${RECEIVED_COLOR( + receivedDiffString, + )}` + ); +}; + const shouldPrintDiff = (expected: unknown, received: unknown): boolean => { const expectedType = getType(expected); const receivedType = getType(received); From 0dd72de8786407c0e8d182a15e4f1773cc3512eb Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Sun, 28 Apr 2019 16:57:48 -0400 Subject: [PATCH 2/6] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64cc33091f84..53e2b33581c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ - `[jest-environment-jsdom]` Re-declare global prototype of JSDOMEnvironment ([#8352](https://github.com/facebook/jest/pull/8352)) - `[jest-snapshot]` Handle arrays when merging snapshots ([#7089](https://github.com/facebook/jest/pull/7089)) - `[expect]` Extract names of async and generator functions ([#8362](https://github.com/facebook/jest/pull/8362)) +- `[expect]` Display expectedDiff more carefully in toBeCloseTo ([#8389](https://github.com/facebook/jest/pull/8389)) ### Chore & Maintenance From 3f22e028b321a0975d61f3c9f1f50d653d622d6e Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Mon, 29 Apr 2019 13:47:52 -0400 Subject: [PATCH 3/6] Add comments about Math method args --- packages/expect/src/print.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/expect/src/print.ts b/packages/expect/src/print.ts index 8cac1a44e823..c69e57a3fc78 100644 --- a/packages/expect/src/print.ts +++ b/packages/expect/src/print.ts @@ -74,9 +74,13 @@ export const printCloseTo = ( ): string => { const receivedDiffString = stringify(receivedDiff); const expectedDiffString = receivedDiffString.includes('e') - ? expectedDiff.toExponential(0) - : 0 <= precision && precision <= 20 - ? expectedDiff.toFixed(precision + 1) + ? // toExponential arg is number of digits after the decimal point. + expectedDiff.toExponential(0) + : 0 <= precision && precision < 20 + ? // toFixed arg is number of digits after the decimal point. + // It may be a value between 0 and 20 inclusive. + // Implementations may optionally support a larger range of values. + expectedDiff.toFixed(precision + 1) : stringify(expectedDiff); return ( From 6023197540abace01adfe37f882e72dad88401e0 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Mon, 29 Apr 2019 13:48:38 -0400 Subject: [PATCH 4/6] Make test names consistent --- .../__snapshots__/matchers.test.js.snap | 520 +++++++++--------- .../expect/src/__tests__/matchers.test.js | 41 +- 2 files changed, 275 insertions(+), 286 deletions(-) diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index 84fbe44944a7..e41c316a6abe 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -452,266 +452,6 @@ Expected: false Received: true" `; -exports[`.toBeCloseTo() {pass: false} expect(-Infinity)toBeCloseTo( -1.23) 1`] = ` -"expect(received).toBeCloseTo(expected) - -Expected: -1.23 -Received: -Infinity - -Expected precision: 2 -Expected difference: < 0.005 -Received difference: Infinity" -`; - -exports[`.toBeCloseTo() {pass: false} expect(3.141592e-7)toBeCloseTo( 3e-7, 8) 1`] = ` -"expect(received).toBeCloseTo(expected, precision) - -Expected: 3e-7 -Received: 3.141592e-7 - -Expected precision: 8 -Expected difference: < 5e-9 -Received difference: 1.4159200000000025e-8" -`; - -exports[`.toBeCloseTo() {pass: false} expect(56789)toBeCloseTo( 51234, -4) 1`] = ` -"expect(received).toBeCloseTo(expected, precision) - -Expected: 51234 -Received: 56789 - -Expected precision: -4 -Expected difference: < 5000 -Received difference: 5555" -`; - -exports[`.toBeCloseTo() {pass: false} expect(Infinity)toBeCloseTo( -Infinity) 1`] = ` -"expect(received).toBeCloseTo(expected) - -Expected: -Infinity -Received: Infinity - -Expected precision: 2 -Expected difference: < 0.005 -Received difference: Infinity" -`; - -exports[`.toBeCloseTo() {pass: false} expect(Infinity)toBeCloseTo( 1.23) 1`] = ` -"expect(received).toBeCloseTo(expected) - -Expected: 1.23 -Received: Infinity - -Expected precision: 2 -Expected difference: < 0.005 -Received difference: Infinity" -`; - -exports[`.toBeCloseTo() {pass: true} expect(-Infinity)toBeCloseTo( -Infinity) 1`] = ` -"expect(received).not.toBeCloseTo(expected) - -Expected: not -Infinity -" -`; - -exports[`.toBeCloseTo() {pass: true} expect(0)toBeCloseTo( 0) 1`] = ` -"expect(received).not.toBeCloseTo(expected) - -Expected: not 0 -" -`; - -exports[`.toBeCloseTo() {pass: true} expect(0)toBeCloseTo( 0.001) 1`] = ` -"expect(received).not.toBeCloseTo(expected) - -Expected: not 0.001 -Received: 0 - -Expected precision: 2 -Expected difference: not < 0.005 -Received difference: 0.001" -`; - -exports[`.toBeCloseTo() {pass: true} expect(1.23)toBeCloseTo( 1.225) 1`] = ` -"expect(received).not.toBeCloseTo(expected) - -Expected: not 1.225 -Received: 1.23 - -Expected precision: 2 -Expected difference: not < 0.005 -Received difference: 0.004999999999999893" -`; - -exports[`.toBeCloseTo() {pass: true} expect(1.23)toBeCloseTo( 1.226) 1`] = ` -"expect(received).not.toBeCloseTo(expected) - -Expected: not 1.226 -Received: 1.23 - -Expected precision: 2 -Expected difference: not < 0.005 -Received difference: 0.0040000000000000036" -`; - -exports[`.toBeCloseTo() {pass: true} expect(1.23)toBeCloseTo( 1.229) 1`] = ` -"expect(received).not.toBeCloseTo(expected) - -Expected: not 1.229 -Received: 1.23 - -Expected precision: 2 -Expected difference: not < 0.005 -Received difference: 0.0009999999999998899" -`; - -exports[`.toBeCloseTo() {pass: true} expect(1.23)toBeCloseTo( 1.234) 1`] = ` -"expect(received).not.toBeCloseTo(expected) - -Expected: not 1.234 -Received: 1.23 - -Expected precision: 2 -Expected difference: not < 0.005 -Received difference: 0.0040000000000000036" -`; - -exports[`.toBeCloseTo() {pass: true} expect(Infinity)toBeCloseTo( Infinity) 1`] = ` -"expect(received).not.toBeCloseTo(expected) - -Expected: not Infinity -" -`; - -exports[`.toBeCloseTo() accepts an optional precision argument: [0, 0.000004, 5] 1`] = ` -"expect(received).not.toBeCloseTo(expected, precision) - -Expected: not 0.000004 -Received: 0 - -Expected precision: 5 -Expected difference: not < 0.000005 -Received difference: 0.000004" -`; - -exports[`.toBeCloseTo() accepts an optional precision argument: [0, 0.0001, 3] 1`] = ` -"expect(received).not.toBeCloseTo(expected, precision) - -Expected: not 0.0001 -Received: 0 - -Expected precision: 3 -Expected difference: not < 0.0005 -Received difference: 0.0001" -`; - -exports[`.toBeCloseTo() accepts an optional precision argument: [0, 0.1, 0] 1`] = ` -"expect(received).not.toBeCloseTo(expected, precision) - -Expected: not 0.1 -Received: 0 - -Expected precision: 0 -Expected difference: not < 0.5 -Received difference: 0.1" -`; - -exports[`.toBeCloseTo() accepts an optional precision argument: [2.0000002, 2, 5] 1`] = ` -"expect(received).not.toBeCloseTo(expected, precision) - -Expected: not 2 -Received: 2.0000002 - -Expected precision: 5 -Expected difference: not < 5e-6 -Received difference: 2.0000000011677344e-7" -`; - -exports[`.toBeCloseTo() throws: [0, 0.01] 1`] = ` -"expect(received).toBeCloseTo(expected) - -Expected: 0.01 -Received: 0 - -Expected precision: 2 -Expected difference: < 0.005 -Received difference: 0.01" -`; - -exports[`.toBeCloseTo() throws: [1, 1.23] 1`] = ` -"expect(received).toBeCloseTo(expected) - -Expected: 1.23 -Received: 1 - -Expected precision: 2 -Expected difference: < 0.005 -Received difference: 0.22999999999999998" -`; - -exports[`.toBeCloseTo() throws: [1.23, 1.2249999] 1`] = ` -"expect(received).toBeCloseTo(expected) - -Expected: 1.2249999 -Received: 1.23 - -Expected precision: 2 -Expected difference: < 0.005 -Received difference: 0.005000099999999952" -`; - -exports[`.toBeCloseTo() throws: Matcher error promise empty isNot false received 1`] = ` -"expect(received).toBeCloseTo(expected, precision) - -Matcher error: received value must be a number - -Received has type: string -Received has value: \\"\\"" -`; - -exports[`.toBeCloseTo() throws: Matcher error promise empty isNot true expected 1`] = ` -"expect(received).not.toBeCloseTo(expected) - -Matcher error: expected value must be a number - -Expected has value: undefined" -`; - -exports[`.toBeCloseTo() throws: Matcher error promise rejects isNot false expected 1`] = ` -"expect(received).rejects.toBeCloseTo(expected) - -Matcher error: expected value must be a number - -Expected has type: string -Expected has value: \\"0\\"" -`; - -exports[`.toBeCloseTo() throws: Matcher error promise rejects isNot true received 1`] = ` -"expect(received).rejects.not.toBeCloseTo(expected) - -Matcher error: received value must be a number - -Received has type: symbol -Received has value: Symbol(0.1)" -`; - -exports[`.toBeCloseTo() throws: Matcher error promise resolves isNot false received 1`] = ` -"expect(received).resolves.toBeCloseTo(expected, precision) - -Matcher error: received value must be a number - -Received has type: boolean -Received has value: false" -`; - -exports[`.toBeCloseTo() throws: Matcher error promise resolves isNot true expected 1`] = ` -"expect(received).resolves.not.toBeCloseTo(expected, precision) - -Matcher error: expected value must be a number - -Expected has value: null" -`; - exports[`.toBeDefined(), .toBeUndefined() '"a"' is defined 1`] = ` "expect(received).not.toBeDefined() @@ -3536,6 +3276,266 @@ Expected: not {\\"test\\": {\\"a\\": 1, \\"b\\": 2}} " `; +exports[`toBeCloseTo {pass: false} expect(-Infinity).toBeCloseTo(-1.23) 1`] = ` +"expect(received).toBeCloseTo(expected) + +Expected: -1.23 +Received: -Infinity + +Expected precision: 2 +Expected difference: < 0.005 +Received difference: Infinity" +`; + +exports[`toBeCloseTo {pass: false} expect(0).toBeCloseTo(0.01) 1`] = ` +"expect(received).toBeCloseTo(expected) + +Expected: 0.01 +Received: 0 + +Expected precision: 2 +Expected difference: < 0.005 +Received difference: 0.01" +`; + +exports[`toBeCloseTo {pass: false} expect(1).toBeCloseTo(1.23) 1`] = ` +"expect(received).toBeCloseTo(expected) + +Expected: 1.23 +Received: 1 + +Expected precision: 2 +Expected difference: < 0.005 +Received difference: 0.22999999999999998" +`; + +exports[`toBeCloseTo {pass: false} expect(1.23).toBeCloseTo(1.2249999) 1`] = ` +"expect(received).toBeCloseTo(expected) + +Expected: 1.2249999 +Received: 1.23 + +Expected precision: 2 +Expected difference: < 0.005 +Received difference: 0.005000099999999952" +`; + +exports[`toBeCloseTo {pass: false} expect(3.141592e-7).toBeCloseTo(3e-7, 8) 1`] = ` +"expect(received).toBeCloseTo(expected, precision) + +Expected: 3e-7 +Received: 3.141592e-7 + +Expected precision: 8 +Expected difference: < 5e-9 +Received difference: 1.4159200000000025e-8" +`; + +exports[`toBeCloseTo {pass: false} expect(56789).toBeCloseTo(51234, -4) 1`] = ` +"expect(received).toBeCloseTo(expected, precision) + +Expected: 51234 +Received: 56789 + +Expected precision: -4 +Expected difference: < 5000 +Received difference: 5555" +`; + +exports[`toBeCloseTo {pass: false} expect(Infinity).toBeCloseTo(-Infinity) 1`] = ` +"expect(received).toBeCloseTo(expected) + +Expected: -Infinity +Received: Infinity + +Expected precision: 2 +Expected difference: < 0.005 +Received difference: Infinity" +`; + +exports[`toBeCloseTo {pass: false} expect(Infinity).toBeCloseTo(1.23) 1`] = ` +"expect(received).toBeCloseTo(expected) + +Expected: 1.23 +Received: Infinity + +Expected precision: 2 +Expected difference: < 0.005 +Received difference: Infinity" +`; + +exports[`toBeCloseTo {pass: true} expect(-Infinity).toBeCloseTo(-Infinity) 1`] = ` +"expect(received).not.toBeCloseTo(expected) + +Expected: not -Infinity +" +`; + +exports[`toBeCloseTo {pass: true} expect(0).toBeCloseTo(0) 1`] = ` +"expect(received).not.toBeCloseTo(expected) + +Expected: not 0 +" +`; + +exports[`toBeCloseTo {pass: true} expect(0).toBeCloseTo(0.000004, 5) 1`] = ` +"expect(received).not.toBeCloseTo(expected, precision) + +Expected: not 0.000004 +Received: 0 + +Expected precision: 5 +Expected difference: not < 0.000005 +Received difference: 0.000004" +`; + +exports[`toBeCloseTo {pass: true} expect(0).toBeCloseTo(0.0001, 3) 1`] = ` +"expect(received).not.toBeCloseTo(expected, precision) + +Expected: not 0.0001 +Received: 0 + +Expected precision: 3 +Expected difference: not < 0.0005 +Received difference: 0.0001" +`; + +exports[`toBeCloseTo {pass: true} expect(0).toBeCloseTo(0.001) 1`] = ` +"expect(received).not.toBeCloseTo(expected) + +Expected: not 0.001 +Received: 0 + +Expected precision: 2 +Expected difference: not < 0.005 +Received difference: 0.001" +`; + +exports[`toBeCloseTo {pass: true} expect(0).toBeCloseTo(0.1, 0) 1`] = ` +"expect(received).not.toBeCloseTo(expected, precision) + +Expected: not 0.1 +Received: 0 + +Expected precision: 0 +Expected difference: not < 0.5 +Received difference: 0.1" +`; + +exports[`toBeCloseTo {pass: true} expect(1.23).toBeCloseTo(1.225) 1`] = ` +"expect(received).not.toBeCloseTo(expected) + +Expected: not 1.225 +Received: 1.23 + +Expected precision: 2 +Expected difference: not < 0.005 +Received difference: 0.004999999999999893" +`; + +exports[`toBeCloseTo {pass: true} expect(1.23).toBeCloseTo(1.226) 1`] = ` +"expect(received).not.toBeCloseTo(expected) + +Expected: not 1.226 +Received: 1.23 + +Expected precision: 2 +Expected difference: not < 0.005 +Received difference: 0.0040000000000000036" +`; + +exports[`toBeCloseTo {pass: true} expect(1.23).toBeCloseTo(1.229) 1`] = ` +"expect(received).not.toBeCloseTo(expected) + +Expected: not 1.229 +Received: 1.23 + +Expected precision: 2 +Expected difference: not < 0.005 +Received difference: 0.0009999999999998899" +`; + +exports[`toBeCloseTo {pass: true} expect(1.23).toBeCloseTo(1.234) 1`] = ` +"expect(received).not.toBeCloseTo(expected) + +Expected: not 1.234 +Received: 1.23 + +Expected precision: 2 +Expected difference: not < 0.005 +Received difference: 0.0040000000000000036" +`; + +exports[`toBeCloseTo {pass: true} expect(2.0000002).toBeCloseTo(2, 5) 1`] = ` +"expect(received).not.toBeCloseTo(expected, precision) + +Expected: not 2 +Received: 2.0000002 + +Expected precision: 5 +Expected difference: not < 5e-6 +Received difference: 2.0000000011677344e-7" +`; + +exports[`toBeCloseTo {pass: true} expect(Infinity).toBeCloseTo(Infinity) 1`] = ` +"expect(received).not.toBeCloseTo(expected) + +Expected: not Infinity +" +`; + +exports[`toBeCloseTo throws: Matcher error promise empty isNot false received 1`] = ` +"expect(received).toBeCloseTo(expected, precision) + +Matcher error: received value must be a number + +Received has type: string +Received has value: \\"\\"" +`; + +exports[`toBeCloseTo throws: Matcher error promise empty isNot true expected 1`] = ` +"expect(received).not.toBeCloseTo(expected) + +Matcher error: expected value must be a number + +Expected has value: undefined" +`; + +exports[`toBeCloseTo throws: Matcher error promise rejects isNot false expected 1`] = ` +"expect(received).rejects.toBeCloseTo(expected) + +Matcher error: expected value must be a number + +Expected has type: string +Expected has value: \\"0\\"" +`; + +exports[`toBeCloseTo throws: Matcher error promise rejects isNot true received 1`] = ` +"expect(received).rejects.not.toBeCloseTo(expected) + +Matcher error: received value must be a number + +Received has type: symbol +Received has value: Symbol(0.1)" +`; + +exports[`toBeCloseTo throws: Matcher error promise resolves isNot false received 1`] = ` +"expect(received).resolves.toBeCloseTo(expected, precision) + +Matcher error: received value must be a number + +Received has type: boolean +Received has value: false" +`; + +exports[`toBeCloseTo throws: Matcher error promise resolves isNot true expected 1`] = ` +"expect(received).resolves.not.toBeCloseTo(expected, precision) + +Matcher error: expected value must be a number + +Expected has value: null" +`; + exports[`toMatchObject() {pass: false} expect([0]).toMatchObject([-0]) 1`] = ` "expect(received).toMatchObject(expected) diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index 70d55df2f534..360ba5e3c70d 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -1058,7 +1058,7 @@ describe('.toContain(), .toContainEqual()', () => { }); }); -describe('.toBeCloseTo()', () => { +describe('toBeCloseTo', () => { [ [0, 0], [0, 0.001], @@ -1066,8 +1066,10 @@ describe('.toBeCloseTo()', () => { [1.23, 1.226], [1.23, 1.225], [1.23, 1.234], + [Infinity, Infinity], + [-Infinity, -Infinity], ].forEach(([n1, n2]) => { - it(`{pass: true} expect(${n1})toBeCloseTo( ${n2})`, () => { + it(`{pass: true} expect(${n1}).toBeCloseTo(${n2})`, () => { jestExpect(n1).toBeCloseTo(n2); expect(() => @@ -1076,29 +1078,16 @@ describe('.toBeCloseTo()', () => { }); }); - [[0, 0.01], [1, 1.23], [1.23, 1.2249999]].forEach(([n1, n2]) => { - it(`throws: [${n1}, ${n2}]`, () => { - expect(() => - jestExpect(n1).toBeCloseTo(n2), - ).toThrowErrorMatchingSnapshot(); - - jestExpect(n1).not.toBeCloseTo(n2); - }); - }); - - [[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( + [ + [0, 0.01], + [1, 1.23], + [1.23, 1.2249999], + [Infinity, -Infinity], + [Infinity, 1.23], + [-Infinity, -1.23], + ].forEach( ([n1, n2]) => { - it(`{pass: false} expect(${n1})toBeCloseTo( ${n2})`, () => { + it(`{pass: false} expect(${n1}).toBeCloseTo(${n2})`, () => { jestExpect(n1).not.toBeCloseTo(n2); expect(() => @@ -1109,7 +1098,7 @@ describe('.toBeCloseTo()', () => { ); [[3.141592e-7, 3e-7, 8], [56789, 51234, -4]].forEach(([n1, n2, p]) => { - it(`{pass: false} expect(${n1})toBeCloseTo( ${n2}, ${p})`, () => { + it(`{pass: false} expect(${n1}).toBeCloseTo(${n2}, ${p})`, () => { jestExpect(n1).not.toBeCloseTo(n2, p); expect(() => @@ -1120,7 +1109,7 @@ describe('.toBeCloseTo()', () => { [[0, 0.1, 0], [0, 0.0001, 3], [0, 0.000004, 5], [2.0000002, 2, 5]].forEach( ([n1, n2, p]) => { - it(`accepts an optional precision argument: [${n1}, ${n2}, ${p}]`, () => { + it(`{pass: true} expect(${n1}).toBeCloseTo(${n2}, ${p})`, () => { jestExpect(n1).toBeCloseTo(n2, p); expect(() => From 7ded2fd9bebb8ca31017201bccf1b73ca6222ea0 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Mon, 29 Apr 2019 14:02:50 -0400 Subject: [PATCH 5/6] Fix pretty lint error --- packages/expect/src/__tests__/matchers.test.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index 360ba5e3c70d..daa41569cdbf 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -1085,17 +1085,15 @@ describe('toBeCloseTo', () => { [Infinity, -Infinity], [Infinity, 1.23], [-Infinity, -1.23], - ].forEach( - ([n1, n2]) => { - it(`{pass: false} expect(${n1}).toBeCloseTo(${n2})`, () => { - jestExpect(n1).not.toBeCloseTo(n2); + ].forEach(([n1, n2]) => { + it(`{pass: false} expect(${n1}).toBeCloseTo(${n2})`, () => { + jestExpect(n1).not.toBeCloseTo(n2); - expect(() => - jestExpect(n1).toBeCloseTo(n2), - ).toThrowErrorMatchingSnapshot(); - }); - }, - ); + expect(() => + jestExpect(n1).toBeCloseTo(n2), + ).toThrowErrorMatchingSnapshot(); + }); + }); [[3.141592e-7, 3e-7, 8], [56789, 51234, -4]].forEach(([n1, n2, p]) => { it(`{pass: false} expect(${n1}).toBeCloseTo(${n2}, ${p})`, () => { From c9cb76f91940306ce602294615a6dd8a6ec46a30 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Thu, 2 May 2019 13:48:15 -0400 Subject: [PATCH 6/6] Correctly update CHANGELOG.md --- CHANGELOG.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b1fe29c901e..e617498f398d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,13 +23,10 @@ - `[jest-environment-jsdom]` Re-declare global prototype of JSDOMEnvironment ([#8352](https://github.com/facebook/jest/pull/8352)) - `[jest-snapshot]` Handle arrays when merging snapshots ([#7089](https://github.com/facebook/jest/pull/7089)) - `[expect]` Extract names of async and generator functions ([#8362](https://github.com/facebook/jest/pull/8362)) -<<<<<<< HEAD - `[expect]` Display expectedDiff more carefully in toBeCloseTo ([#8389](https://github.com/facebook/jest/pull/8389)) -======= - `[jest-runtime]` Fix virtual mocks not being unmockable after previously being mocked ([#8396](https://github.com/facebook/jest/pull/8396)) - `[jest-transform]` Replace special characters in transform cache filenames to support Windows ([#8353](https://github.com/facebook/jest/pull/8353)) - `[jest-config]` Allow exactly one project ([#7498](https://github.com/facebook/jest/pull/7498)) ->>>>>>> master ### Chore & Maintenance