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

Enhancement/expect-to-be-close-to-with-infinity #7444

Merged
merged 10 commits into from
Dec 2, 2018
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)` works as intended aswell as with `_Infinity` ([#7405](https://github.com/facebook/jest/pull/7405))
joao-conde marked this conversation as resolved.
Show resolved Hide resolved

### 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]) => {
joao-conde marked this conversation as resolved.
Show resolved Hide resolved
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
11 changes: 10 additions & 1 deletion packages/expect/src/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,16 @@ 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 both actual and expected are equal to the Infinity global
joao-conde marked this conversation as resolved.
Show resolved Hide resolved
if (actual == Infinity && expected == Infinity) pass = true;
//If both actual and expected are equal to the symmetric of the Infinity global
else if (actual == -Infinity && expected == -Infinity) pass = true;
//both actual and expected are not equal to the Infinity global
//evaluate if they are close to each other taking precision into account
else pass = Math.abs(expected - actual) < Math.pow(10, -precision) / 2;

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