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

expect: Display expectedDiff more carefully in toBeCloseTo #8389

Merged
merged 8 commits into from
Aug 19, 2019

Conversation

pedrottimark
Copy link
Contributor

Summary

Because Node 12.0.0 can display distracting precision for expectedDiff

  1. if the default stringify value of receivedDiff is exponential notation, call toExponential
  2. otherwise if precision is non-negative and within ECMA-262 range, call toFixed
  3. otherwise call 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

@pedrottimark
Copy link
Contributor Author

pedrottimark commented Apr 28, 2019

Example pictures baseline at left and improved at right

Prevent regression with toFixed:

true 5 toFixed

Prevent regression with toExponential when received value has exponent:

true 5 toExponential

Same as current baseline with toExponential which is same as stringify:

false 8 toExponential

Same as current baseline with stringify and illustrate negative precision:

false -4 stringify

@pedrottimark pedrottimark changed the title expect: Display expectedDiff more carefully expect: Display expectedDiff more carefully in toBeCloseTo Apr 28, 2019
@pedrottimark pedrottimark mentioned this pull request Apr 28, 2019
2 tasks
@codecov-io
Copy link

codecov-io commented Apr 28, 2019

Codecov Report

Merging #8389 into master will increase coverage by 0.03%.
The diff coverage is 100%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
packages/expect/src/matchers.ts 96.8% <100%> (+0.01%) ⬆️
packages/expect/src/print.ts 98.07% <100%> (+0.34%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4941d12...c9cb76f. Read the comment docs.

Copy link
Contributor

@jeysal jeysal left a comment

Choose a reason for hiding this comment

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

Two small requests :)

packages/expect/src/print.ts Outdated Show resolved Hide resolved
packages/expect/src/__tests__/matchers.test.js Outdated Show resolved Hide resolved
@pedrottimark
Copy link
Contributor Author

@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

@pedrottimark
Copy link
Contributor Author

Let’s wait for #8382 and then I will either resolve conflicts or open a new PR

@SimenB
Copy link
Member

SimenB commented Aug 19, 2019

@pedrottimark could you rebase this and land it? 🙏

@SimenB
Copy link
Member

SimenB commented Aug 19, 2019

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,

@pedrottimark
Copy link
Contributor Author

pedrottimark commented Aug 19, 2019

I updated snapshots using Node.js 10.16.0 and then reran matcher tests locally using 12.8.1

@github-actions
Copy link

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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants