Skip to content

Commit

Permalink
Enhancement/expect-to-be-close-to-with-infinity (#7444)
Browse files Browse the repository at this point in the history
* toBeCloseTo works properly when comparing with Infinity

* CHANGELOG update

* Linter issue solved

* Apply suggestions from code review

Co-Authored-By: joao-conde <up201503256@fe.up.pt>

* Update matchers.js

* Update CHANGELOG

* Fixed bug, leftover of old comment

* Removed unecessary self-explanatory comments

* Updated CHANGELOG

* Snapshot updates
  • Loading branch information
João Dias Conde Azevedo authored and rickhanlonii committed Dec 2, 2018
1 parent 9671afa commit 624ac56
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- `[jest-config]` Add `dependencyExtractor` option to use a custom module to extract dependencies from files ([#7313](https://github.com/facebook/jest/pull/7313), [#7349](https://github.com/facebook/jest/pull/7349), [#7350](https://github.com/facebook/jest/pull/7350))
- `[jest-haste-map]` Accept a `getCacheKey` method in `hasteImplModulePath` modules to reset the cache when the logic changes ([#7350](https://github.com/facebook/jest/pull/7350))
- `[jest-config]` Add `haste.computeSha1` option to compute the sha-1 of the files in the haste map ([#7345](https://github.com/facebook/jest/pull/7345))
- `[expect]` `expect(Infinity).toBeCloseTo(Infinity)` Treats `Infinity` as equal in toBeCloseTo matcher ([#7405](https://github.com/facebook/jest/pull/7405))

### Fixes

Expand Down
40 changes: 40 additions & 0 deletions packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,38 @@ Expected: <green>false</>
Received: <red>true</>"
`;

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

Precision: <green>2</>-digit
Expected: <green>-1.23</>
Received: <red>-Infinity</>"
`;

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

Precision: <green>2</>-digit
Expected: <green>-Infinity</>
Received: <red>Infinity</>"
`;

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

Precision: <green>2</>-digit
Expected: <green>1.23</>
Received: <red>Infinity</>"
`;

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

Precision: <green>2</>-digit
Expected: <green>-Infinity</>
Received: <red>-Infinity</>"
`;

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

Expand Down Expand Up @@ -416,6 +448,14 @@ Expected: <green>1.234</>
Received: <red>1.23</>"
`;

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

Precision: <green>2</>-digit
Expected: <green>Infinity</>
Received: <red>Infinity</>"
`;

exports[`.toBeCloseTo() accepts an optional precision argument: [0, 0.000004, 5] 1`] = `
"<dim>expect(</><red>received</><dim>).</>not<dim>.toBeCloseTo(</><green>expected</><dim>, </><green>precision</><dim>)</>

Expand Down
22 changes: 22 additions & 0 deletions packages/expect/src/__tests__/matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,28 @@ describe('.toBeCloseTo()', () => {
});
});

[[Infinity, Infinity], [-Infinity, -Infinity]].forEach(([n1, n2]) => {
it(`{pass: true} expect(${n1})toBeCloseTo( ${n2})`, () => {
jestExpect(n1).toBeCloseTo(n2);

expect(() =>
jestExpect(n1).not.toBeCloseTo(n2),
).toThrowErrorMatchingSnapshot();
});
});

[[Infinity, -Infinity], [Infinity, 1.23], [-Infinity, -1.23]].forEach(
([n1, n2]) => {
it(`{pass: false} expect(${n1})toBeCloseTo( ${n2})`, () => {
jestExpect(n1).not.toBeCloseTo(n2);

expect(() =>
jestExpect(n1).toBeCloseTo(n2),
).toThrowErrorMatchingSnapshot();
});
},
);

[[0, 0.1, 0], [0, 0.0001, 3], [0, 0.000004, 5]].forEach(([n1, n2, p]) => {
it(`accepts an optional precision argument: [${n1}, ${n2}, ${p}]`, () => {
jestExpect(n1).toBeCloseTo(n2, p);
Expand Down
8 changes: 7 additions & 1 deletion packages/expect/src/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,13 @@ const matchers: MatchersObject = {
toBeCloseTo(actual: number, expected: number, precision?: number = 2) {
const secondArgument = arguments.length === 3 ? 'precision' : null;
ensureNumbers(actual, expected, '.toBeCloseTo');
const pass = Math.abs(expected - actual) < Math.pow(10, -precision) / 2;

let pass = false;

if (actual == Infinity && expected == Infinity) pass = true;
else if (actual == -Infinity && expected == -Infinity) pass = true;
else pass = Math.abs(expected - actual) < Math.pow(10, -precision) / 2;

const message = () =>
matcherHint('.toBeCloseTo', undefined, undefined, {
isNot: this.isNot,
Expand Down

0 comments on commit 624ac56

Please sign in to comment.