-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
expect: Display expectedDiff more carefully in toBeCloseTo #8389
Conversation
Example pictures baseline at left and improved at right Prevent regression with toFixed: Prevent regression with toExponential when received value has exponent: Same as current baseline with toExponential which is same as stringify: Same as current baseline with stringify and illustrate negative precision: |
Codecov Report
@@ Coverage Diff @@
## master #8389 +/- ##
==========================================
+ Coverage 62.32% 62.35% +0.03%
==========================================
Files 266 266
Lines 10733 10742 +9
Branches 2614 2619 +5
==========================================
+ Hits 6689 6698 +9
Misses 3461 3461
Partials 583 583
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two small requests :)
@jeysal What do you think about follow up PR during breaking change phase before Jest 25: Matcher error: expected precision must be an integer The new formatting is incorrect if precision is decimal, but I cannot think of a use case for that |
Let’s wait for #8382 and then I will either resolve conflicts or open a new PR |
@pedrottimark could you rebase this and land it? 🙏 |
Does this one look correct? diff --git c/CHANGELOG.md w/CHANGELOG.md
index 51ca552dc..accab0fc7 100644
--- c/CHANGELOG.md
+++ w/CHANGELOG.md
@@ -4,6 +4,7 @@
### Fixes
+- `[expect]` Display expectedDiff more carefully in toBeCloseTo ([#8389](https://github.com/facebook/jest/pull/8389))
- `[jest-fake-timers]` `getTimerCount` will not include cancelled immediates ([#8764](https://github.com/facebook/jest/pull/8764))
### Chore & Maintenance
diff --git c/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap w/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap
index 45aa73441..2153e90ee 100644
--- c/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap
+++ w/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap
@@ -469,233 +469,6 @@ Expected: <green>false</>
Received: <red>true</>"
`;
-exports[`.toBeCloseTo() {pass: false} expect(-Infinity)toBeCloseTo( -1.23) 1`] = `
-"<dim>expect(</><red>received</><dim>).</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
-
-Expected: <green>-1.23</>
-Received: <red>-Infinity</>
-
-Expected precision: 2
-Expected difference: < <green>0.005</>
-Received difference: <red>Infinity</>"
-`;
-
-exports[`.toBeCloseTo() {pass: false} expect(Infinity)toBeCloseTo( -Infinity) 1`] = `
-"<dim>expect(</><red>received</><dim>).</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
-
-Expected: <green>-Infinity</>
-Received: <red>Infinity</>
-
-Expected precision: 2
-Expected difference: < <green>0.005</>
-Received difference: <red>Infinity</>"
-`;
-
-exports[`.toBeCloseTo() {pass: false} expect(Infinity)toBeCloseTo( 1.23) 1`] = `
-"<dim>expect(</><red>received</><dim>).</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
-
-Expected: <green>1.23</>
-Received: <red>Infinity</>
-
-Expected precision: 2
-Expected difference: < <green>0.005</>
-Received difference: <red>Infinity</>"
-`;
-
-exports[`.toBeCloseTo() {pass: true} expect(-Infinity)toBeCloseTo( -Infinity) 1`] = `
-"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
-
-Expected: not <green>-Infinity</>
-"
-`;
-
-exports[`.toBeCloseTo() {pass: true} expect(0)toBeCloseTo( 0) 1`] = `
-"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
-
-Expected: not <green>0</>
-"
-`;
-
-exports[`.toBeCloseTo() {pass: true} expect(0)toBeCloseTo( 0.001) 1`] = `
-"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
-
-Expected: not <green>0.001</>
-Received: <red>0</>
-
-Expected precision: 2
-Expected difference: not < <green>0.005</>
-Received difference: <red>0.001</>"
-`;
-
-exports[`.toBeCloseTo() {pass: true} expect(1.23)toBeCloseTo( 1.225) 1`] = `
-"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
-
-Expected: not <green>1.225</>
-Received: <red>1.23</>
-
-Expected precision: 2
-Expected difference: not < <green>0.005</>
-Received difference: <red>0.004999999999999893</>"
-`;
-
-exports[`.toBeCloseTo() {pass: true} expect(1.23)toBeCloseTo( 1.226) 1`] = `
-"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
-
-Expected: not <green>1.226</>
-Received: <red>1.23</>
-
-Expected precision: 2
-Expected difference: not < <green>0.005</>
-Received difference: <red>0.0040000000000000036</>"
-`;
-
-exports[`.toBeCloseTo() {pass: true} expect(1.23)toBeCloseTo( 1.229) 1`] = `
-"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
-
-Expected: not <green>1.229</>
-Received: <red>1.23</>
-
-Expected precision: 2
-Expected difference: not < <green>0.005</>
-Received difference: <red>0.0009999999999998899</>"
-`;
-
-exports[`.toBeCloseTo() {pass: true} expect(1.23)toBeCloseTo( 1.234) 1`] = `
-"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
-
-Expected: not <green>1.234</>
-Received: <red>1.23</>
-
-Expected precision: 2
-Expected difference: not < <green>0.005</>
-Received difference: <red>0.0040000000000000036</>"
-`;
-
-exports[`.toBeCloseTo() {pass: true} expect(Infinity)toBeCloseTo( Infinity) 1`] = `
-"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
-
-Expected: not <green>Infinity</>
-"
-`;
-
-exports[`.toBeCloseTo() accepts an optional precision argument: [0, 0.000004, 5] 1`] = `
-"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>, </>precision<dim>)</>
-
-Expected: not <green>0.000004</>
-Received: <red>0</>
-
-Expected precision: 5
-Expected difference: not < <green>0.000005</>
-Received difference: <red>0.000004</>"
-`;
-
-exports[`.toBeCloseTo() accepts an optional precision argument: [0, 0.0001, 3] 1`] = `
-"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>, </>precision<dim>)</>
-
-Expected: not <green>0.0001</>
-Received: <red>0</>
-
-Expected precision: 3
-Expected difference: not < <green>0.0005</>
-Received difference: <red>0.0001</>"
-`;
-
-exports[`.toBeCloseTo() accepts an optional precision argument: [0, 0.1, 0] 1`] = `
-"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>, </>precision<dim>)</>
-
-Expected: not <green>0.1</>
-Received: <red>0</>
-
-Expected precision: 0
-Expected difference: not < <green>0.5</>
-Received difference: <red>0.1</>"
-`;
-
-exports[`.toBeCloseTo() throws: [0, 0.01] 1`] = `
-"<dim>expect(</><red>received</><dim>).</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
-
-Expected: <green>0.01</>
-Received: <red>0</>
-
-Expected precision: 2
-Expected difference: < <green>0.005</>
-Received difference: <red>0.01</>"
-`;
-
-exports[`.toBeCloseTo() throws: [1, 1.23] 1`] = `
-"<dim>expect(</><red>received</><dim>).</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
-
-Expected: <green>1.23</>
-Received: <red>1</>
-
-Expected precision: 2
-Expected difference: < <green>0.005</>
-Received difference: <red>0.22999999999999998</>"
-`;
-
-exports[`.toBeCloseTo() throws: [1.23, 1.2249999] 1`] = `
-"<dim>expect(</><red>received</><dim>).</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
-
-Expected: <green>1.2249999</>
-Received: <red>1.23</>
-
-Expected precision: 2
-Expected difference: < <green>0.005</>
-Received difference: <red>0.005000099999999952</>"
-`;
-
-exports[`.toBeCloseTo() throws: Matcher error promise empty isNot false received 1`] = `
-"<dim>expect(</><red>received</><dim>).</>toBeCloseTo<dim>(</><green>expected</><dim>, </>precision<dim>)</>
-
-<bold>Matcher error</>: <red>received</> value must be a number
-
-Received has type: string
-Received has value: <red>\\"\\"</>"
-`;
-
-exports[`.toBeCloseTo() throws: Matcher error promise empty isNot true expected 1`] = `
-"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
-
-<bold>Matcher error</>: <green>expected</> value must be a number
-
-Expected has value: <green>undefined</>"
-`;
-
-exports[`.toBeCloseTo() throws: Matcher error promise rejects isNot false expected 1`] = `
-"<dim>expect(</><red>received</><dim>).</>rejects<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
-
-<bold>Matcher error</>: <green>expected</> value must be a number
-
-Expected has type: string
-Expected has value: <green>\\"0\\"</>"
-`;
-
-exports[`.toBeCloseTo() throws: Matcher error promise rejects isNot true received 1`] = `
-"<dim>expect(</><red>received</><dim>).</>rejects<dim>.</>not<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
-
-<bold>Matcher error</>: <red>received</> value must be a number
-
-Received has type: symbol
-Received has value: <red>Symbol(0.1)</>"
-`;
-
-exports[`.toBeCloseTo() throws: Matcher error promise resolves isNot false received 1`] = `
-"<dim>expect(</><red>received</><dim>).</>resolves<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>, </>precision<dim>)</>
-
-<bold>Matcher error</>: <red>received</> value must be a number
-
-Received has type: boolean
-Received has value: <red>false</>"
-`;
-
-exports[`.toBeCloseTo() throws: Matcher error promise resolves isNot true expected 1`] = `
-"<dim>expect(</><red>received</><dim>).</>resolves<dim>.</>not<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>, </>precision<dim>)</>
-
-<bold>Matcher error</>: <green>expected</> value must be a number
-
-Expected has value: <green>null</>"
-`;
-
exports[`.toBeDefined(), .toBeUndefined() '"a"' is defined 1`] = `
"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toBeDefined<dim>()</>
@@ -3609,6 +3382,266 @@ Expected: not <green>{\\"test\\": {\\"a\\": 1, \\"b\\": 2}}</>
"
`;
+exports[`toBeCloseTo {pass: false} expect(-Infinity).toBeCloseTo(-1.23) 1`] = `
+"<dim>expect(</><red>received</><dim>).</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
+
+Expected: <green>-1.23</>
+Received: <red>-Infinity</>
+
+Expected precision: 2
+Expected difference: < <green>0.005</>
+Received difference: <red>Infinity</>"
+`;
+
+exports[`toBeCloseTo {pass: false} expect(0).toBeCloseTo(0.01) 1`] = `
+"<dim>expect(</><red>received</><dim>).</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
+
+Expected: <green>0.01</>
+Received: <red>0</>
+
+Expected precision: 2
+Expected difference: < <green>0.005</>
+Received difference: <red>0.01</>"
+`;
+
+exports[`toBeCloseTo {pass: false} expect(1).toBeCloseTo(1.23) 1`] = `
+"<dim>expect(</><red>received</><dim>).</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
+
+Expected: <green>1.23</>
+Received: <red>1</>
+
+Expected precision: 2
+Expected difference: < <green>0.005</>
+Received difference: <red>0.22999999999999998</>"
+`;
+
+exports[`toBeCloseTo {pass: false} expect(1.23).toBeCloseTo(1.2249999) 1`] = `
+"<dim>expect(</><red>received</><dim>).</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
+
+Expected: <green>1.2249999</>
+Received: <red>1.23</>
+
+Expected precision: 2
+Expected difference: < <green>0.005</>
+Received difference: <red>0.005000099999999952</>"
+`;
+
+exports[`toBeCloseTo {pass: false} expect(3.141592e-7).toBeCloseTo(3e-7, 8) 1`] = `
+"<dim>expect(</><red>received</><dim>).</>toBeCloseTo<dim>(</><green>expected</><dim>, </>precision<dim>)</>
+
+Expected: <green>3e-7</>
+Received: <red>3.141592e-7</>
+
+Expected precision: 8
+Expected difference: < <green>5e-9</>
+Received difference: <red>1.4159200000000025e-8</>"
+`;
+
+exports[`toBeCloseTo {pass: false} expect(56789).toBeCloseTo(51234, -4) 1`] = `
+"<dim>expect(</><red>received</><dim>).</>toBeCloseTo<dim>(</><green>expected</><dim>, </>precision<dim>)</>
+
+Expected: <green>51234</>
+Received: <red>56789</>
+
+Expected precision: -4
+Expected difference: < <green>5000</>
+Received difference: <red>5555</>"
+`;
+
+exports[`toBeCloseTo {pass: false} expect(Infinity).toBeCloseTo(-Infinity) 1`] = `
+"<dim>expect(</><red>received</><dim>).</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
+
+Expected: <green>-Infinity</>
+Received: <red>Infinity</>
+
+Expected precision: 2
+Expected difference: < <green>0.005</>
+Received difference: <red>Infinity</>"
+`;
+
+exports[`toBeCloseTo {pass: false} expect(Infinity).toBeCloseTo(1.23) 1`] = `
+"<dim>expect(</><red>received</><dim>).</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
+
+Expected: <green>1.23</>
+Received: <red>Infinity</>
+
+Expected precision: 2
+Expected difference: < <green>0.005</>
+Received difference: <red>Infinity</>"
+`;
+
+exports[`toBeCloseTo {pass: true} expect(-Infinity).toBeCloseTo(-Infinity) 1`] = `
+"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
+
+Expected: not <green>-Infinity</>
+"
+`;
+
+exports[`toBeCloseTo {pass: true} expect(0).toBeCloseTo(0) 1`] = `
+"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
+
+Expected: not <green>0</>
+"
+`;
+
+exports[`toBeCloseTo {pass: true} expect(0).toBeCloseTo(0.000004, 5) 1`] = `
+"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>, </>precision<dim>)</>
+
+Expected: not <green>0.000004</>
+Received: <red>0</>
+
+Expected precision: 5
+Expected difference: not < <green>0.000005</>
+Received difference: <red>0.000004</>"
+`;
+
+exports[`toBeCloseTo {pass: true} expect(0).toBeCloseTo(0.0001, 3) 1`] = `
+"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>, </>precision<dim>)</>
+
+Expected: not <green>0.0001</>
+Received: <red>0</>
+
+Expected precision: 3
+Expected difference: not < <green>0.0005</>
+Received difference: <red>0.0001</>"
+`;
+
+exports[`toBeCloseTo {pass: true} expect(0).toBeCloseTo(0.001) 1`] = `
+"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
+
+Expected: not <green>0.001</>
+Received: <red>0</>
+
+Expected precision: 2
+Expected difference: not < <green>0.005</>
+Received difference: <red>0.001</>"
+`;
+
+exports[`toBeCloseTo {pass: true} expect(0).toBeCloseTo(0.1, 0) 1`] = `
+"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>, </>precision<dim>)</>
+
+Expected: not <green>0.1</>
+Received: <red>0</>
+
+Expected precision: 0
+Expected difference: not < <green>0.5</>
+Received difference: <red>0.1</>"
+`;
+
+exports[`toBeCloseTo {pass: true} expect(1.23).toBeCloseTo(1.225) 1`] = `
+"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
+
+Expected: not <green>1.225</>
+Received: <red>1.23</>
+
+Expected precision: 2
+Expected difference: not < <green>0.005</>
+Received difference: <red>0.004999999999999893</>"
+`;
+
+exports[`toBeCloseTo {pass: true} expect(1.23).toBeCloseTo(1.226) 1`] = `
+"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
+
+Expected: not <green>1.226</>
+Received: <red>1.23</>
+
+Expected precision: 2
+Expected difference: not < <green>0.005</>
+Received difference: <red>0.0040000000000000036</>"
+`;
+
+exports[`toBeCloseTo {pass: true} expect(1.23).toBeCloseTo(1.229) 1`] = `
+"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
+
+Expected: not <green>1.229</>
+Received: <red>1.23</>
+
+Expected precision: 2
+Expected difference: not < <green>0.005</>
+Received difference: <red>0.0009999999999998899</>"
+`;
+
+exports[`toBeCloseTo {pass: true} expect(1.23).toBeCloseTo(1.234) 1`] = `
+"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
+
+Expected: not <green>1.234</>
+Received: <red>1.23</>
+
+Expected precision: 2
+Expected difference: not < <green>0.005</>
+Received difference: <red>0.0040000000000000036</>"
+`;
+
+exports[`toBeCloseTo {pass: true} expect(2.0000002).toBeCloseTo(2, 5) 1`] = `
+"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>, </>precision<dim>)</>
+
+Expected: not <green>2</>
+Received: <red>2.0000002</>
+
+Expected precision: 5
+Expected difference: not < <green>5e-6</>
+Received difference: <red>2.0000000011677344e-7</>"
+`;
+
+exports[`toBeCloseTo {pass: true} expect(Infinity).toBeCloseTo(Infinity) 1`] = `
+"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
+
+Expected: not <green>Infinity</>
+"
+`;
+
+exports[`toBeCloseTo throws: Matcher error promise empty isNot false received 1`] = `
+"<dim>expect(</><red>received</><dim>).</>toBeCloseTo<dim>(</><green>expected</><dim>, </>precision<dim>)</>
+
+<bold>Matcher error</>: <red>received</> value must be a number
+
+Received has type: string
+Received has value: <red>\\"\\"</>"
+`;
+
+exports[`toBeCloseTo throws: Matcher error promise empty isNot true expected 1`] = `
+"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
+
+<bold>Matcher error</>: <green>expected</> value must be a number
+
+Expected has value: <green>undefined</>"
+`;
+
+exports[`toBeCloseTo throws: Matcher error promise rejects isNot false expected 1`] = `
+"<dim>expect(</><red>received</><dim>).</>rejects<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
+
+<bold>Matcher error</>: <green>expected</> value must be a number
+
+Expected has type: string
+Expected has value: <green>\\"0\\"</>"
+`;
+
+exports[`toBeCloseTo throws: Matcher error promise rejects isNot true received 1`] = `
+"<dim>expect(</><red>received</><dim>).</>rejects<dim>.</>not<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>)</>
+
+<bold>Matcher error</>: <red>received</> value must be a number
+
+Received has type: symbol
+Received has value: <red>Symbol(0.1)</>"
+`;
+
+exports[`toBeCloseTo throws: Matcher error promise resolves isNot false received 1`] = `
+"<dim>expect(</><red>received</><dim>).</>resolves<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>, </>precision<dim>)</>
+
+<bold>Matcher error</>: <red>received</> value must be a number
+
+Received has type: boolean
+Received has value: <red>false</>"
+`;
+
+exports[`toBeCloseTo throws: Matcher error promise resolves isNot true expected 1`] = `
+"<dim>expect(</><red>received</><dim>).</>resolves<dim>.</>not<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>, </>precision<dim>)</>
+
+<bold>Matcher error</>: <green>expected</> value must be a number
+
+Expected has value: <green>null</>"
+`;
+
exports[`toMatchObject() {pass: false} expect([0]).toMatchObject([-0]) 1`] = `
"<dim>expect(</><red>received</><dim>).</>toMatchObject<dim>(</><green>expected</><dim>)</>
diff --git c/packages/expect/src/__tests__/matchers.test.js w/packages/expect/src/__tests__/matchers.test.js
index e7b683898..a61bdd28a 100644
--- c/packages/expect/src/__tests__/matchers.test.js
+++ w/packages/expect/src/__tests__/matchers.test.js
@@ -1110,7 +1110,7 @@ describe('.toContain(), .toContainEqual()', () => {
});
});
-describe('.toBeCloseTo()', () => {
+describe('toBeCloseTo', () => {
[
[0, 0],
[0, 0.001],
@@ -1118,8 +1118,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(() =>
@@ -1128,48 +1130,45 @@ describe('.toBeCloseTo()', () => {
});
});
- [[0, 0.01], [1, 1.23], [1.23, 1.2249999]].forEach(([n1, n2]) => {
- it(`throws: [${n1}, ${n2}]`, () => {
+ [
+ [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})`, () => {
+ jestExpect(n1).not.toBeCloseTo(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);
+ [[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),
+ jestExpect(n1).toBeCloseTo(n2, p),
).toThrowErrorMatchingSnapshot();
});
});
- [[Infinity, -Infinity], [Infinity, 1.23], [-Infinity, -1.23]].forEach(
- ([n1, n2]) => {
- it(`{pass: false} expect(${n1})toBeCloseTo( ${n2})`, () => {
- jestExpect(n1).not.toBeCloseTo(n2);
+ [[0, 0.1, 0], [0, 0.0001, 3], [0, 0.000004, 5], [2.0000002, 2, 5]].forEach(
+ ([n1, n2, p]) => {
+ it(`{pass: true} expect(${n1}).toBeCloseTo(${n2}, ${p})`, () => {
+ jestExpect(n1).toBeCloseTo(n2, p);
expect(() =>
- jestExpect(n1).toBeCloseTo(n2),
+ jestExpect(n1).not.toBeCloseTo(n2, p),
).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);
-
- expect(() =>
- jestExpect(n1).not.toBeCloseTo(n2, p),
- ).toThrowErrorMatchingSnapshot();
- });
- });
-
describe('throws: Matcher error', () => {
test('promise empty isNot false received', () => {
const precision = 3;
diff --git c/packages/expect/src/matchers.ts w/packages/expect/src/matchers.ts
index c9ee95c4f..c461fee88 100644
--- c/packages/expect/src/matchers.ts
+++ w/packages/expect/src/matchers.ts
@@ -27,6 +27,7 @@ import {
} from 'jest-matcher-utils';
import {MatcherState, MatchersObject} from './types';
import {
+ printCloseTo,
printExpectedConstructorName,
printExpectedConstructorNameNot,
printReceivedArrayContainExpectedItem,
@@ -129,8 +130,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,
secondArgumentColor: (arg: string) => arg,
@@ -160,18 +162,14 @@ const matchers: MatchersObject = {
? ''
: `Received: ${printReceived(received)}\n` +
'\n' +
- `Expected precision: ${stringify(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: ${stringify(precision)}\n` +
- `Expected difference: < ${printExpected(expectedDiff)}\n` +
- `Received difference: ${printReceived(receivedDiff)}`;
+ printCloseTo(receivedDiff, expectedDiff, precision, isNot);
return {message, pass};
},
diff --git c/packages/expect/src/print.ts w/packages/expect/src/print.ts
index aa0360139..4772c96c5 100644
--- c/packages/expect/src/print.ts
+++ w/packages/expect/src/print.ts
@@ -61,6 +61,36 @@ export const printReceivedArrayContainExpectedItem = (
']',
);
+export const printCloseTo = (
+ receivedDiff: number,
+ expectedDiff: number,
+ precision: number,
+ isNot: boolean,
+): string => {
+ const receivedDiffString = stringify(receivedDiff);
+ const expectedDiffString = receivedDiffString.includes('e')
+ ? // 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 (
+ `Expected precision: ${isNot ? ' ' : ''} ${stringify(
+ precision,
+ )}\n` +
+ `Expected difference: ${isNot ? 'not ' : ''}< ${EXPECTED_COLOR(
+ expectedDiffString,
+ )}\n` +
+ `Received difference: ${isNot ? ' ' : ''} ${RECEIVED_COLOR(
+ receivedDiffString,
+ )}`
+ );
+};
+
export const printExpectedConstructorName = (
label: string,
expected: Function, |
I updated snapshots using Node.js 10.16.0 and then reran matcher tests locally using 12.8.1 |
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Summary
Because Node 12.0.0 can display distracting precision for
expectedDiff
receivedDiff
is exponential notation, calltoExponential
toFixed
stringify
(baseline)Test plan
Added 3 snapshot tests
Tested locally in Node 8.12.0, 10.15.3, and 12.0.0
See also pictures for 12.0.0 in following comment