Skip to content

Commit

Permalink
chore: update default precision from jest-closeTo
Browse files Browse the repository at this point in the history
close #23
BREAKING CHANGE: changes the precision from 1*10^-x to be 5*10^-(x+1)
  • Loading branch information
maasencioh committed Sep 28, 2021
1 parent 28a8eac commit 880d084
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 17 deletions.
7 changes: 4 additions & 3 deletions src/__tests__/deepClose.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('toBeDeepCloseTo', () => {
expect(42.0003).toBeDeepCloseTo(42.0004, 3);
});

it('numbers defaut digits', () => {
it('numbers default digits', () => {
expect(42).toBeDeepCloseTo(42);
});

Expand Down Expand Up @@ -75,8 +75,9 @@ describe('fails', () => {
expect(42.03).not.toBeDeepCloseTo(42.0004, 3);
});

it('numbers defaut digits', () => {
expect(42.0003).not.toBeDeepCloseTo(42.0004);
it('numbers default digits', () => {
expect(42.03).not.toBeCloseTo(42.04);
expect(42.03).not.toBeDeepCloseTo(42.04);
});

it('strings', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/matchClose.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ describe('fails', () => {
expect(42.03).not.toMatchCloseTo(42.0004, 3);
});

it('numbers defaut digits', () => {
expect(42.0003).not.toMatchCloseTo(42.0004);
it('numbers default digits', () => {
expect(42.03).not.toMatchCloseTo(42.04);
});

it('strings', () => {
Expand Down
10 changes: 2 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,17 @@ function printResponse(
export function toBeDeepCloseTo(
received: Iterable,
expected: Iterable,
decimals?: number,
decimals = 2,
): MatcherResult {
if (decimals === undefined) {
decimals = 10;
}
const error = recursiveCheck(received, expected, decimals);
return printResponse(error, received, expected);
}

export function toMatchCloseTo(
received: Iterable,
expected: Iterable,
decimals?: number,
decimals = 2,
): MatcherResult {
if (decimals === undefined) {
decimals = 10;
}
const error = recursiveCheck(received, expected, decimals, false);
return printResponse(error, received, expected);
}
8 changes: 4 additions & 4 deletions src/recursiveCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function recursiveCheck(
expected,
received,
};
} else if (Math.abs(received - expected) <= Math.pow(10, -decimals)) {
} else if (Math.abs(received - expected) <= 0.5 * Math.pow(10, -decimals)) {
return false;
} else {
return {
Expand Down Expand Up @@ -117,9 +117,9 @@ export function recursiveCheck(
const sameLength = !strict || receivedKeys.length === expectedKeys.length;
if (
!sameLength ||
expectedKeys.some(function (e) {
return !Object.prototype.hasOwnProperty.call(received, e);
})
expectedKeys.some(
(e) => !Object.prototype.hasOwnProperty.call(received, e),
)
) {
return {
reason: 'The objects do not have similar keys',
Expand Down

0 comments on commit 880d084

Please sign in to comment.