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

Make BN matchers compatible with chai's lengthOf #423

Merged
merged 2 commits into from
Jan 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
});
});
});