Skip to content

Commit

Permalink
🌂 closeTo actual convert to BN (#724)
Browse files Browse the repository at this point in the history
  • Loading branch information
yivlad authored May 6, 2022
1 parent 2ccc395 commit 1e598c5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/eighty-squids-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ethereum-waffle/chai": patch
---

BN closeTo actual may be a js number
2 changes: 1 addition & 1 deletion waffle-chai/src/matchers/bigNumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function overwriteBigNumberCloseTo(_super: (...args: any[]) => any, chaiUtils: C
BigNumber.from(expected).sub(actual).abs().lte(delta),
`Expected "${expected}" to be within ${delta} of ${actual}`,
`Expected "${expected}" NOT to be within ${delta} of ${actual}`,
`A number between ${actual.sub(delta)} and ${actual.sub(delta)}`,
`A number between ${BigNumber.from(actual).sub(delta)} and ${BigNumber.from(actual).sub(delta)}`,
expected
);
} else {
Expand Down
44 changes: 24 additions & 20 deletions waffle-chai/test/matchers/bigNumber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,32 +141,36 @@ describe('UNIT: BigNumber matchers', () => {

describe('closeTo', () => {
it('.to.be.closeTo', () => {
expect(BigNumber.from(100)).to.be.closeTo(BigNumber.from(101), 10);
expect(BigNumber.from(100)).to.be.closeTo(BigNumber.from(101), BigNumber.from(10));
checkAll(100, 101, (a, b) => expect(a).to.be.closeTo(b, 10));
checkAll(100, 101, (a, b) => expect(a).to.be.closeTo(b, BigNumber.from(10)));
});

it('.not.to.be.closeTo', () => {
expect(BigNumber.from(100)).not.to.be.closeTo(BigNumber.from(111), 10);
expect(BigNumber.from(100)).not.to.be.closeTo(BigNumber.from(111), BigNumber.from(10));
checkAll(100, 111, (a, b) => expect(a).not.to.be.closeTo(b, 10));
checkAll(100, 111, (a, b) => expect(a).not.to.be.closeTo(b, BigNumber.from(10)));
});

it('expect to throw on error', () => {
expect(() => expect(BigNumber.from(100)).to.be.closeTo(BigNumber.from(111), 10)).to.throw(
AssertionError,
'Expected "100" to be within 10 of 111'
);
expect(() => expect(BigNumber.from(100)).to.be.closeTo(BigNumber.from(111), BigNumber.from(10))).to.throw(
AssertionError,
'Expected "100" to be within 10 of 111'
);
expect(() => expect(BigNumber.from(100)).not.to.be.closeTo(BigNumber.from(101), 10)).to.throw(
AssertionError,
'Expected "100" NOT to be within 10 of 101'
);
expect(() => expect(BigNumber.from(100)).not.to.be.closeTo(BigNumber.from(101), BigNumber.from(10))).to.throw(
AssertionError,
'Expected "100" NOT to be within 10 of 101'
);
checkAll(100, 111, (a, b) => {
expect(() => expect(BigNumber.from(a)).to.be.closeTo(BigNumber.from(b), 10)).to.throw(
AssertionError,
'Expected "100" to be within 10 of 111'
);
expect(() => expect(BigNumber.from(a)).to.be.closeTo(BigNumber.from(b), BigNumber.from(10))).to.throw(
AssertionError,
'Expected "100" to be within 10 of 111'
);
});
checkAll(100, 101, (a, b) => {
expect(() => expect(BigNumber.from(a)).not.to.be.closeTo(BigNumber.from(b), 10)).to.throw(
AssertionError,
'Expected "100" NOT to be within 10 of 101'
);
expect(() => expect(BigNumber.from(a)).not.to.be.closeTo(BigNumber.from(b), BigNumber.from(10))).to.throw(
AssertionError,
'Expected "100" NOT to be within 10 of 101'
);
});
});
});
});

0 comments on commit 1e598c5

Please sign in to comment.