Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -2,6 +2,7 @@

### Features

- `[expect]` `expect(Infinity).toBeCloseTo(Infinity)` works as intended aswell as with `_Infinity` ([#7405](https://github.com/facebook/jest/pull/7405))
- `[jest-cli]` [**BREAKING**] Only set error process error codes when they are non-zero ([#7363](https://github.com/facebook/jest/pull/7363))
- `[jest-config]` [**BREAKING**] Deprecate `setupTestFrameworkScriptFile` in favor of new `setupFilesAfterEnv` ([#7119](https://github.com/facebook/jest/pull/7119))
- `[jest-worker]` [**BREAKING**] Add functionality to call a `setup` method in the worker before the first call and a `teardown` method when ending the farm ([#7014](https://github.com/facebook/jest/pull/7014))
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]) => {
Comment thread
joao-conde marked this conversation as 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
10 changes: 9 additions & 1 deletion packages/expect/src/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,15 @@ 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 Infinity it is true they are close
Comment thread
joao-conde marked this conversation as resolved.
Outdated
if (actual == Infinity && expected == Infinity) pass = true;
//If both actual and expected are -Infinity it is true they are close
else if (actual == -Infinity && expected == -Infinity) pass = true;
//actual and expected 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