Skip to content

Commit

Permalink
Make BN matchers compatible with chai's lengthOf (#423)
Browse files Browse the repository at this point in the history
Co-authored-by: Marek Kirejczyk <marekkirejczyk@users.noreply.github.com>
  • Loading branch information
vanruch and marekkirejczyk committed Jan 17, 2021
1 parent 666cdc6 commit 5d74803
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions waffle-chai/src/matchers/bigNumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ function overwriteBigNumberFunction(
return function (this: Chai.AssertionStatic, ...args: any[]) {
const [actual] = args;
const expected = chaiUtils.flag(this, 'object');
if (chaiUtils.flag(this, 'doLength') && BigNumber.isBigNumber(actual)) {
_super.apply(this, [actual.toNumber()]);
return;
}
if (BigNumber.isBigNumber(expected) || BigNumber.isBigNumber(actual)) {
this.assert(
BigNumber.from(expected)[functionName](actual),
Expand Down
17 changes: 17 additions & 0 deletions waffle-chai/test/matchers/misc.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {expect, AssertionError} from 'chai';
import {ethers} from 'ethers';

describe('UNIT: Miscellaneous', () => {
describe('Proper address', () => {
Expand Down Expand Up @@ -80,4 +81,20 @@ describe('UNIT: Miscellaneous', () => {
).to.throw(AssertionError, 'Expected "0x70" not to be a proper hex of length 2, but it was');
});
});

describe('Other chai matchers compatibility', () => {
it('lengthOf works', async () => {
const one = ethers.constants.One;
const list = [1, 2, 3];

expect(one).to.be.at.least(1);

expect(list.length).to.be.at.least(one);
expect(list).to.have.lengthOf.at.least(1);
expect([1]).to.have.lengthOf.at.least(one);
expect(list).to.have.lengthOf.at.least(one);
expect(list).to.have.lengthOf.at.most(one.mul(3));
expect(() => expect(list).to.have.lengthOf.at.most(one)).to.throw;
});
});
});

0 comments on commit 5d74803

Please sign in to comment.