Skip to content

Commit

Permalink
expect: Display expectedDiff more carefully in toBeCloseTo (#8389)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrottimark authored and SimenB committed Aug 19, 2019
1 parent b09de2d commit 08f109c
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 112 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
193 changes: 113 additions & 80 deletions packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ Expected: <green>false</>
Received: <red>true</>"
`;

exports[`.toBeCloseTo() {pass: false} expect(-Infinity)toBeCloseTo( -1.23) 1`] = `
exports[`.toBeCloseTo {pass: false} expect(-Infinity).toBeCloseTo(-1.23) 1`] = `
"<dim>expect(</><red>received</><dim>).</>toBeCloseTo<dim>(</><green>expected</><dim>)</>

Expected: <green>-1.23</>
Expand All @@ -480,7 +480,62 @@ Expected difference: < <green>0.005</>
Received difference: <red>Infinity</>"
`;

exports[`.toBeCloseTo() {pass: false} expect(Infinity)toBeCloseTo( -Infinity) 1`] = `
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</>
Expand All @@ -491,7 +546,7 @@ Expected difference: < <green>0.005</>
Received difference: <red>Infinity</>"
`;

exports[`.toBeCloseTo() {pass: false} expect(Infinity)toBeCloseTo( 1.23) 1`] = `
exports[`.toBeCloseTo {pass: false} expect(Infinity).toBeCloseTo(1.23) 1`] = `
"<dim>expect(</><red>received</><dim>).</>toBeCloseTo<dim>(</><green>expected</><dim>)</>

Expected: <green>1.23</>
Expand All @@ -502,21 +557,43 @@ Expected difference: < <green>0.005</>
Received difference: <red>Infinity</>"
`;

exports[`.toBeCloseTo() {pass: true} expect(-Infinity)toBeCloseTo( -Infinity) 1`] = `
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`] = `
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`] = `
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</>
Expand All @@ -527,7 +604,18 @@ Expected difference: not < <green>0.005</>
Received difference: <red>0.001</>"
`;

exports[`.toBeCloseTo() {pass: true} expect(1.23)toBeCloseTo( 1.225) 1`] = `
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</>
Expand All @@ -538,7 +626,7 @@ Expected difference: not < <green>0.005</>
Received difference: <red>0.004999999999999893</>"
`;

exports[`.toBeCloseTo() {pass: true} expect(1.23)toBeCloseTo( 1.226) 1`] = `
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</>
Expand All @@ -549,7 +637,7 @@ Expected difference: not < <green>0.005</>
Received difference: <red>0.0040000000000000036</>"
`;

exports[`.toBeCloseTo() {pass: true} expect(1.23)toBeCloseTo( 1.229) 1`] = `
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</>
Expand All @@ -560,7 +648,7 @@ Expected difference: not < <green>0.005</>
Received difference: <red>0.0009999999999998899</>"
`;

exports[`.toBeCloseTo() {pass: true} expect(1.23)toBeCloseTo( 1.234) 1`] = `
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</>
Expand All @@ -571,80 +659,25 @@ 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`] = `
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>0.000004</>
Received: <red>0</>
Expected: not <green>2</>
Received: <red>2.0000002</>

Expected precision: 5
Expected difference: not < <green>0.000005</>
Received difference: <red>0.000004</>"
Expected difference: not < <green>5e-6</>
Received difference: <red>2.0000000011677344e-7</>"
`;

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</>
exports[`.toBeCloseTo {pass: true} expect(Infinity).toBeCloseTo(Infinity) 1`] = `
"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toBeCloseTo<dim>(</><green>expected</><dim>)</>

Expected precision: 2
Expected difference: < <green>0.005</>
Received difference: <red>0.005000099999999952</>"
Expected: not <green>Infinity</>
"
`;

exports[`.toBeCloseTo() throws: Matcher error promise empty isNot false received 1`] = `
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
Expand All @@ -653,15 +686,15 @@ Received has type: string
Received has value: <red>\\"\\"</>"
`;

exports[`.toBeCloseTo() throws: Matcher error promise empty isNot true expected 1`] = `
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`] = `
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
Expand All @@ -670,7 +703,7 @@ Expected has type: string
Expected has value: <green>\\"0\\"</>"
`;

exports[`.toBeCloseTo() throws: Matcher error promise rejects isNot true received 1`] = `
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
Expand All @@ -679,7 +712,7 @@ Received has type: symbol
Received has value: <red>Symbol(0.1)</>"
`;

exports[`.toBeCloseTo() throws: Matcher error promise resolves isNot false received 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
Expand All @@ -688,7 +721,7 @@ Received has type: boolean
Received has value: <red>false</>"
`;

exports[`.toBeCloseTo() throws: Matcher error promise resolves isNot true expected 1`] = `
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
Expand Down
Loading

0 comments on commit 08f109c

Please sign in to comment.