Chai assertions for comparing arbitrary-precision decimals using the decimal.js library. Forked from chai-bn (originally forked from chai-bignumber), which uses the bn.js library (originally the bignumber.js library).
npm install --save-dev chai-decimaljs
const chai = require('chai'); const Decimal = require('decimal.js'); // Enable and inject Decimal dependency chai.use(require('chai-decimaljs')(Decimal));
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:
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
BigNumber instances for both actual and expected values.
Only BDD style (
should) assertions are supported.
const actual = new Decimal('100').plus(new Decimal('0.0000000000000000000000000000001')); const expected = '100.0000000000000000000000000000001'; actual.should.be.a.decimal.that.equals(expected); expect(actual).to.be.a.decimal.that.is.at.most(expected); (new Decimal('1999.999999999999999999')).should.be.a.decimal.that.is.lessThan('2000');
(new Decimal('-100.102')).should.be.a.decimal.that.is.negative; expect(new Decimal('1.33').sub(new Decimal('1.33'))).to.be.a.decimal.that.is.zero;
Chai properties (e.g. the
that.is chain) have no effect other than increasing readability, and can be dropped if less verbosity is desired.