chai-ethers-bn
![NPM Package](https://img.shields.io/npm/v/chai-ethers-bn.svg?style=flat-square)
Chai
assertions for comparing arbitrary-precision integers using the BigNumber
library from ethers. Forked from
chai-bn.
Installation
npm install --save-dev chai-ethers-bn
Usage
import chai from 'chai';
import { BigNumber } from 'ethers';
import chaiEthersBN from 'chai-ethers-bn';
chai.use(chaiEthersBN());
Assertions
The following assertion methods are provided and will override the existing builtin assertions if the bignumber
property is set as part of the assertion chain:
- equal/equals/eq
- above/gt/greaterThan
- least/gte
- below/lt/lessThan
- most/lte
- closeTo
A set of additional assertion properties is also provided:
Both actual values (the values being asserted) and expected values (the values the actual value is expected to match) can be either instances of BN
, or strings which can be converted into a valid number. This is a key difference with chai-bignumber, which automatically converts JavaScript numbers to BigNumber
instances for both actual and expected values.
Only BDD style (expect
or should
) assertions are supported.
Examples
Methods:
const actual = BigNumber.from('100000000000000000').plus(BigNumber.from('1'));
const expected = '100000000000000001';
actual.should.be.a.bignumber.that.equals(expected);
expect(actual).to.be.a.bignumber.that.is.at.most(expected);
(BigNumber.from('1000')).should.be.a.bignumber.that.is.lessThan('2000');
Properties:
BigNumber.from('-100').should.be.a.bignumber.that.is.negative;
expect(BigNumber.from('1').sub(BigNumber.from('1'))).to.be.a.bignumber.that.is.zero;
To make things shorter you can also use the alias bn
:
actual.should.be.a.bn.that.equals(expected);
Some Chai
properties (e.g. the that.is
chain) have no effect other than increasing readability, and can be dropped if less verbosity is desired.
License
MIT